开发者

How to configure model activerecord in rails for items with multiple links

I have two models, Item and Link. Each link is going to join two items together, only two. What is the best way to set up the model relationship?

I currently have this:

class Item < ActiveRecord::Base
  has_many  :links
end


class Link < ActiveRecord::Base
  belongs_to :item1, :class_name => "Item", :foreign_key => "item1_name"
  belongs_to :item2, :class_name => "Item", :foreign_key => "item2_name"
end

Is t开发者_运维技巧his correct? Will I run into any problems later on when I wanted to list the links for each item?

Please recommend me better alternatives, Thanks.


take a look at has one through

Something like

Item

:has_one :item, through: :links

That should link two items together through the links model

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜