如何在 Rails 中为具有多个链接的项目配置模型 ActiveRecord
我有两个模型,项目和链接。每个链接都会将两个项目连接在一起,只有两个。建立模型关系的最佳方式是什么?
我目前有这个:
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
这是正确的吗?当我想列出每个项目的链接时,以后会遇到任何问题吗?
请推荐我更好的选择,谢谢。
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 this 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看有一个
类似的东西
应该通过链接模型将两个项目链接在一起
take a look at has one through
Something like
That should link two items together through the links model