t.references in the migration vs belongs_to in the model?

This is a fairly recent addition to Rails, so it may not be covered in the book you mention. You can read about it in the migration section of Rails Guides.

When you generate using, say,

rails generate model Thing name post:references

… the migration will create the foreign key field for you, as well as create the index. That’s what t.references does.

You could have written

rails generate model Thing name post_id:integer:index

and gotten the same end result.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)