如何定义一个named_scope来汇总一个具有嵌套模型的has_many?

发布于 2024-08-20 09:01:58 字数 775 浏览 9 评论 0原文

首先是数据模型:

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

因此用户可以将主题发布到论坛。他们还可以将评论发布到 论坛中的主题。他们可以发布对评论的回复。

我希望能够获得他们参加过的论坛列表 发布主题或评论或回复。

First the data model:

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

So Users can post Topics to Forums. They can also post Comments to the
Topics in a Forum. And they can post Replies to the Comments.

I want to be able to get a list of Forums they've participated in by
posting either Topics or Comments or Replies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2024-08-27 09:01:58

您正在寻找的是论坛模型中的命名范围

通过在论坛模型中为 Comment 和 Reply 模型添加 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

现在...

Forum.user_posted(@user)

将返回用户发布主题、回复或评论的论坛数组。

对于特定用户已发布的论坛列表:

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)

将返回已按主题、评论或回复在论坛中发布的用户列表。

PS:在这个模型中允许破坏用户可能不是一个好主意。按照您安排的方式,如果用户被破坏,他们发布的任何主题、回复或评论也将被删除。相反,您的用户应该被停用。

What you're looking for is a named scope in the Forum model.

The join can be greatly simplified by adding has_many :through relationships for your Comment and Reply models in the Forum model. But I can never remember how nested joins work out, so I've posted a solution that will work with what you've got.

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

Now...

Forum.user_posted(@user)

Will return an array of forums where a user has posted a topic, reply or comment.

For a list of forums a particular user has posted in:

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

Assuming I got that right, this statement:

User.posted_in_forum(@forum)

will return a list of users that have posted in forum either by topic, comment or reply.

P.S. it's probably not a good idea to allow destruction of users in this model. The way you've laid things out, if a user is destroyed, any topics, replies or comments they posted will also be removed. Instead your users should be deactivated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文