Ruby on rails 如何定义一个命名的作用域来汇总具有嵌套模型的多个对象?

Ruby on rails 如何定义一个命名的作用域来汇总具有嵌套模型的多个对象?,ruby-on-rails,activerecord,associations,has-many,Ruby On Rails,Activerecord,Associations,Has Many,首先是数据模型: class Forum < ActiveRecord::Base has_many :topics, :dependent => :destroy, :order => 'created_at desc' end class User < ActiveRecord::Base has_many :topics, :dependent => :destroy has_many :comments, :dependent => :d

首先是数据模型:

class Forum < ActiveRecord::Base
  has_many :topics, :dependent => :destroy, :order => 'created_at desc'
end

class User < ActiveRecord::Base
  has_many :topics, :dependent => :destroy
  has_many :comments, :dependent => :destroy
  has_many :replies, :dependent => :destroy
end

class Topic < ActiveRecord::Base
  belongs_to :forum
  belongs_to :user
  has_many :comments, :dependent => :destroy
end

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :topic
  has_many :replies, :dependent => :destroy
end

class Reply < ActiveRecord::Base
  belongs_to :user
  belongs_to :comment
end
class论坛:destroy,:order=>'created\u at desc'
结束
类用户:销毁
有很多:注释,:依赖=>:销毁
有多个:回复,:依赖=>:销毁
结束
类主题:销毁
结束
类注释:销毁
结束
类回复
因此用户可以将主题发布到论坛。他们也可以在网站上发表评论 论坛中的主题。他们可以发布对评论的回复

我希望能够得到一份他们参加过的论坛的名单
发布主题、评论或回复

你要找的是论坛模式中的一个

通过在论坛模型中为你的评论和回复模型添加has_many:through关系,加入可以大大简化。但我永远记不起嵌套联接是如何工作的,所以我发布了一个解决方案,它将与您现有的解决方案一起工作

class Forum < ActiveRecord::Base
  has_many :topics, :dependent => :destroy, :order => 'created_at desc'
  named_scope :user_posted, lambda {|user|
    { :joins => "JOIN topics t ON t.forum_id = forums.id " +
        "JOIN comments c ON c.topic_id = t.id " +
        "JOIN replies r ON r.comment_id = c.id", 
      :conditions => ["t.user_id = ? OR c.user_id = ? OR r.user_id = ?", 
        user, user, user], 
      :group => "forums.id"
    }
  }
end
将返回一组论坛,其中用户发布了主题、回复或评论

有关特定用户在其中发布的论坛列表:

class User < ActiveRecord::Base
  has_many :topics, :dependent => :destroy
  has_many :comments, :dependent => :destroy
  has_many :replies, :dependent => :destroy    
  named_scope :posted_in_forum, lambda {|forum|
    { :joins => "JOIN replies r ON r.user_id = users.id "+
        "JOIN comments c ON c.user_id = users.id OR c.id = r.comment_id " +
        "JOIN topics t ON t.user_id = users.id OR t.id = c.topic_id " +
        "JOIN forums f ON t.forum_id = forums.id ",
      :conditions => ["f.id = ?", forum], 
      :group => "users.id"
    }
  }
end
将按主题、评论或回复返回已在论坛中发布的用户列表


另外,在这种模式下,允许销毁用户可能不是一个好主意。按照您的布局方式,如果用户被销毁,他们发布的任何主题、回复或评论也将被删除。相反,应该停用您的用户。

太好了!Thx的优秀和快速的答复。我必须按“to”:group编辑您的“:group”,但除此之外,它提供了我所需要的内容。我知道它必须通过一个名为_scope的地方。而且,是的,我同意你的pt关于用户停用与销毁的观点。再次使用Thx。后续问题:如何获取已发布到特定论坛的用户列表?我已更新解决方案以更正:组!=:错误地将_分组,并包含一个用于解决后续问题的命名范围。附言:如果你觉得答案有帮助,你应该投赞成票。如果它解决了你的问题,你应该接受它。这样,其他有类似问题的人会发现更容易找到正确的解决方案。再次感谢您的快速响应。而且。。。我已经接受了答案,但我想我没有投票支持某件事所需的15分-(嗯…在SQL“ActiveRecord::StatementInvalid(Mysql::error:您的SQL语法有错误;…”上获取错误,但我不知道原因是什么
class User < ActiveRecord::Base
  has_many :topics, :dependent => :destroy
  has_many :comments, :dependent => :destroy
  has_many :replies, :dependent => :destroy    
  named_scope :posted_in_forum, lambda {|forum|
    { :joins => "JOIN replies r ON r.user_id = users.id "+
        "JOIN comments c ON c.user_id = users.id OR c.id = r.comment_id " +
        "JOIN topics t ON t.user_id = users.id OR t.id = c.topic_id " +
        "JOIN forums f ON t.forum_id = forums.id ",
      :conditions => ["f.id = ?", forum], 
      :group => "users.id"
    }
  }
end
User.posted_in_forum(@forum)