views:

167

answers:

1

I'm a bit confused as to how "relationships" are created/processed in ROR/rake.

Can anyone confirm if the following is correct?

  1. Code the "insert xxx relation" in the DB Migration file. Migrate this once done.
  2. Code in the final relationship (has_xxx...) in the model file.

If so, can ROR autogen the DB Migration file from changes in the model?

A: 

You are on the right path.
In your migration, use the 't.references' method. For example:

t.references :user

This will create a 'user_id' column in your database.
Then in your model, you use:

belongs_to :user

If you want to use 'has_xxx :widgets' in your model, the widgets database table needs the 'whatever_id' column.
Rails does not make any changes to your migration based on your model. AFAIK, Rails will never change your migration after you create it. You are going to have to code these relationships by hand.

GhettoDuk
I think the OP was asking if rails could auto-generate the t.references based on the belongs_to in the model.
klochner