【发布时间】:2016-04-20 16:22:20
【问题描述】:
我正在尝试使用user.comments 访问给定用户的所有 cmets。查询将通过两个不同的模型,它们可能都返回结果。我的关系设置如下:
class User < ActiveRecord::Base
has_many :organisers
has_many :participants
has_many :comments, through: :participants / :organisers (see explenation below)
end
class Organiser < ActiveRecord::Base
belongs_to :user
end
class Participant < ActiveRecord::Base
belongs_to :user
end
class Comment < ActiveRecord::Base
belongs_to :organiser
belongs_to :participant
end
验证评论属于参与者或组织者。
我不知道该怎么做。我试过了
has_many :comments, through: :participants
has_many :comments, through: :organisers
和
has_many :comments, through: [:organisers, :participants]
但最后一个不是铁轨。有没有合适的方法来做到这一点?谢谢!
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 activerecord