Reputation: 522
Suppose I have Model Person and Model Book. One person has many books. I have a has_many books in Person's model and belongs_to in Book's model. When I generate those scaffold, there're no foreign keys in the database. Do I have to always create a migration using the convention, for example book_id, manually ? Why rails does not link thing on the database ? I didn't figure out how Rails knots everything together.
Upvotes: 1
Views: 1998
Reputation: 6029
If you want to enforce database foreign keys, you have to use a gem like foreigner
Upvotes: 1
Reputation: 2084
Or what is the same:
rails g scaffold book name:string person:references
this will create a person_id field in the book table.
Upvotes: 4
Reputation: 30453
There is no reason for rails to make any assumption about that. You may create foreign key with scaffold, or fix migration by yourself before run it.
rails generate scaffold book name:string person_id:integer
Upvotes: 1