1
votes

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.

3

3 Answers

4
votes

Or what is the same:

rails g scaffold book name:string person:references

this will create a person_id field in the book table.

1
votes

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
1
votes

If you want to enforce database foreign keys, you have to use a gem like foreigner