I was searching google and stackoverflow for long time, but I can't find the solution of my problem.
Lately, I was using set_primary_key for a table called "employee", because I need to use ther personnel number as my primary key. If I set the code
`set_primary_key :personel_number`
(Personel_number is already a collumn which I want to use as primary key) into my model before I do rake db:migrate and do migrating at last, I come into troubles when I try to fill my database via browser:
`Couldn't find employee with ID=1`
`app/controllers/mitarbeiters_controller.rb:16:in `show'`
Rails searches for employee with ID=1 but it can't find, because I set primary key from personel_number with 601 (e.g.).
Can I do something against it or shall I let Rails create it's own :id first?
It sounds like when the page goes to the controller to create the employee you have a redirect to show the new employee and it is using the default id.
Related
I have existing rails app that have some tables with some data. I did the CRUD operation directly from postgresql client before using activeadmin.
I don't know whether I missed the documentation or this is a bug: activeadmin cannot detect my existing autoincrement id for table.
If I refresh the submitted form until the auto increment id surpass the existing id in my table, it works.
First think which I could think of would be that you have passed the id parameter in permit params.
Please check that and if is present then remove it.
Secondly,as mentioned in the post that there are already data in the database so there is a problem with the sequences generated, since they can be only used once.
The solution is to set the sequence for your song_artists.id column to the highest value in the table with a query like this:
SELECT setval('song_artist_id_seq', (SELECT max(id) FROM song_artists));
I am assuming that your sequence name "song_artist_id_seq", table name "song_artist", and column name "id".
To get the sequence name run the below mentioned command:
SELECT pg_get_serial_sequence('tablename', 'columname');
For Resetting the postgres sequences from rails console:
ActiveRecord::Base.connection.tables.each do |t|
ActiveRecord::Base.connection.reset_pk_sequence!(t)
end
Another solution would be to override the save() method in your song_artist class to manually set the song_artist id for new records but is not advisable.
I have a controller called Edit an existing client which does what the name implies. It will load up a client of that name and pull all relevant information if it exists. In the view I have
#client = Client.find_by(:id => 1)
and it loads all the information up correctly for anything that has_one association to Client. However for something like address which a Client has_many of, it does not show at all in the view. How would I fix this?
Example output
The address stuff should show up under number of dependents but it does not.
This is the view
And here is the table for addresses
Here is the controller for client
Here are the models
UPDATE Thank you bntzio. I am actually dumb. It is now pulling the types of addresses as it should, however the form allows you to add a former address and a mailing address. This is handled by a series of bools in the table. Right now it just pulls all three addresses and puts them under "Present Address". How would I add logic to pull the correct address for that section?
Sample output here
There are two things to consider here, the first one:
You should have a column for address in your database. Check your db migrations.
It should look like this:
t.string :address
If you don't have that table in your db, then you can't access that variable holding the address, so you need to add it with a migration.
rails g migration AddAddressToUsers address:string
or
rails g migration AddAddressToClients address:string (depending on how you have it).
Then do a migration to migrate the changes:
rake db:migrate
Reload your server and add the proper code in the view and it should be displayed correctly.
The second thing is:
Check your controller if it contains strong parameters, if yes, then add your :address variable (anyway you need to have that variable in your db, as mentioned before).
I'm working through the Rails Crash Course book, and the project is to create a social tumblr style clone. I've created a User model with just a name and id (and already migrated), and now I'm trying to implement the following/subscription style that tumblr does. The book has the following to create a new model and migration:
bin/rails g model Subscription leader:references follower:references
The thought is the leader will the the person followed, and the follower is obviously the follower. Both will reference the id from the User table though.
The problem is with the migration. I get the following:
PG::UndefinedTable: ERROR: relation "leaders" does not exist
: ALTER TABLE "subscriptions" ADD CONSTRAINT "fk_rails_7b4891a3a6"
FOREIGN KEY ("leader_id")
REFERENCES "leaders" ("id")
Plus a bunch of other stuff that looks almost exactly the same.
While I'm assuming that it's throwing an error because no leader table with it's own id exists, is there a way to fix the migration to reference the user.id for each reference, or will I just manually need to create each column?
I think it's a Postgres specific problem. You can bypass it by using:
bin/rails g model Subscription leader_id:integer:index follower_id:integer:index
I'm currently looking at migrating an existing system (written in spaghetti PHP) over to rails. The problem is, it has to run off of a live database. A lot of the ID columns on these different tables aren't named id. For instance, the customers table has an ID column called Customer_ID. Upon looking, I just realised that rails does infact seem to find by the primary key instead of a specific column called id.
Will I face a lot of problems later with the naming of these ID columns, specifically in stuff like relationships?
After v2.3.8, set_primary_key :col_name is deprecated.
self.primary_key = 'col_name' is recommended.
http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/PrimaryKey/ClassMethods.html
Change primary key attribute in model by using
set_primary_key :col_name
I have two RoR3 application:
http://users.domain.local
http://profiles.domain.local
I created the 'users/models/profile.rb':
class Profile < ActiveResource::Base
self.site = "http://profiles.domain.local"
end
In 'profiles/models/profile.rb' I have:
attr_accessible :name
My profile's SQL table contains these columns:
id
name
user_id
So if I run Profile.create(:name => "test_name") a new profile will be created in http://profiles.domain.local with the name "test_name".
For obvious security reasons, I don't want to make accessible the 'user_id' attribute, but I need to set that on the profile creation from the 'users' application.
I tryed a lot of way do make that, but I can't find an easy solution. Maybe, it is possible with an if statement near the 'attr_accessible' of the 'profile' application that fill a request from the 'user' application.
Can somebody help me?
You could try something like what Amazon Web Services does: use a very long, randomly generated key with each request. Check that key is correct in your profiles app, and if yes, update the attribute.
Solution: Don't use simply Profile.create, use the association builders instead. Protect the user_id attribute and use user.profiles.create!(params[:profile]) to have it automatically set the user_id field for profiles to whatever the user object is.