Sam D20
Sam D20

Reputation: 2575

When to assign a foreign key and when to just create an association?

I'm learning about model associations in rails. I've learned how to give the table for a model a column to hold foreign keys of another model like so:

rails generate model User account_id:integer 

I would then take the primary key of an account from an Account table and assign it to the account_id for the designated user.

However, I am also told to create the association in User.rb like so:

has_one :account

I understand the difference between these two things. One creates a column in the table (the first line after the migration), and the other generates a series of helpers (the latter).

However, what I am seeing in tutorials is that sometimes both are done, while other times, only the association (has_one :account) is done. How do I go about deciding when to create a column in the table to hold foreign keys, and when to just create the association in the model .rb file?

Upvotes: 2

Views: 624

Answers (2)

jefflunt
jefflunt

Reputation: 33954

  1. Your first example is a scaffold that creates the model file and the migration.
  2. has_one helps ActiveRecord understand the relationships between the tables so that it can, among other things, generate proper SQL queries for you.

But #1 has to be in place for #2 to even work. However, creating the db column via a scaffold command isn't necessary - it's just convenient sometimes, because it creates both the model file, and the associated migration.

You could just write the migration by hand. Just because whatever thing you're following doesn't always mention adding the db column doesn't mean it's not necessary. It's probably just assumed that you've already done it because these foreign key migrations are so common after you get up and running with Rails that they sort of go without saying after a while.

Upvotes: 2

MrYoshiji
MrYoshiji

Reputation: 54882

  • You will always have to create the column in the Database (1)
  • It is not mandatory to define the relation(s) inside the models, but it is highly recommended (2)

(1) : The Database needs this column of foreign keys to be able to retrieve the corresponding record. Without the column, the DB cannot find back the related record. You can use a Migration to create this column, not only a scaffold.

(2) : You can skip the relations declaration in the models, but it is highly recommended because:

  • it generate methods corresponding to the relations (ex: User belongs_to :role, then you can do user.role directly instead of Role.where(id: user.role_id).first)
  • not every human can remember all the associations. It is better to show/list everything that is linked to your model

You asked:

How do I go about deciding when to create a column in the table to hold foreign keys, and when to just create the association in the model .rb file?

I would answer:

Always create the column (cannot work if you don't) AND define every association(s) (relation(s)) inside the model.

Upvotes: 2

Related Questions