我应该为相同类型的关系创建不同的联接表吗?
我有可以由多态协会投票的帖子。现在我也发表评论。我可以为我的评论分享相同的模型和逻辑吗?或者我必须建立一种新的关系模式吗?
#post.rb
has_many :votes, :as => :votable
has_many :voting_users,
:through => :votes,
:source => :user
#vote.rb
belongs_to :votable, :polymorphic => true
I have posts that can be voted on by a polymorphic association. Now I am making comments votable as well. Can I share the same models and logic for this for my comments? Or do I have to make a new model relationship ?
#post.rb
has_many :votes, :as => :votable
has_many :voting_users,
:through => :votes,
:source => :user
#vote.rb
belongs_to :votable, :polymorphic => true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您应该能够从帖子模型中复制两个 has_many 关系并将它们放入 comment.rb 中,不会出现任何问题。由于您的投票模型是多态的,因此只要
votes
表中有votable_id:integer
和votable_type:string
,一切都应该正常工作。Yes, you should be able to copy the two has_many relationships from your post model and drop them in comment.rb without a problem. Since your vote model is polymorphic, as long as you have
votable_id:integer
andvotable_type:string
in thevotes
table, everything should work normally.