我试图建立一种基本上如下的关系:
组模型
has_and_belongs_to_many :users
has_many :posts
用户模型
has_and_belongs_to_many :groups
has_many :posts
邮政模型
belongs_to :group
belongs_to :user
当我查询用户的帖子时,可以执行user.posts。但是,我无法弄清楚如何从用户加入的组中查询所有帖子。任何建议表示赞赏!
你要
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
has_many :posts
has_many :group_posts, through: :groups, source: :posts
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句