Rails: is there a difference between "links: foo" and "integer: foo_id"?

When I use references :foo in the carry, the generated column is called foo_id . Is there a difference between doing references :foo and just doing integer :foo_id ? Maybe something happens under the hood to provide connectivity at the database level?

+6
ruby-on-rails activerecord migration
source share
2 answers

The result will be the same for your particular case; you're right. But references allows a parameter :polymorphic => true , which automatically creates the foo_type column as a row in the table.

Semantically, references better if you are trying to make your migrations better reflect the relationships between the tables in the database.

+8
source share

@Mike's answer perfectly explains the meaning of references . However, it is often better not to link your migrations too close to your AR associations. In particular, you can access all kinds of brine when it comes to deploying your application, if you run your migrations before updating the application from version control, for example. It doesn't really matter until he bites you :-)

+2
source share

All Articles