Ruby On Rails ORM Model Relations
I'm a bit confused as to how "relationships" are created/processed in ROR/rake.
Can开发者_如何学编程 anyone confirm if the following is correct?
- Code the "insert xxx relation" in the DB Migration file. Migrate this once done.
- 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?
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.
精彩评论