开发者

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?

  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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜