59

在 Rails 中是否可以有多个has_many :through相互传递的关系?我收到了这样做的建议,作为我发布的另一个问题的解决方案,但一直无法让它发挥作用。

朋友是通过连接表的循环关联。目标是创建一个has_many :throughfor friends_comments,所以我可以采取 aUser并做一些类似的事情user.friends_comments来获取他的朋友在一个查询中发表的所有评论。

class User
  has_many :friendships
  has_many :friends, 
           :through => :friendships,
           :conditions => "status = #{Friendship::FULL}"
  has_many :comments
  has_many :friends_comments, :through => :friends, :source => :comments
end

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"
end

这看起来很棒,也很有意义,但对我不起作用。这是我在尝试访问用户的friends_comments 时在相关部分遇到的错误:
ERROR: column users.user_id does not exist
: SELECT "comments".* FROM "comments" INNER JOIN "users" ON "comments".user_id = "users".id WHERE (("users".user_id = 1) AND ((status = 2)))

当我输入 user.friends 时,它可以工作,这是它执行的查询:
: SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users".id = "friendships".friend_id WHERE (("friendships".user_id = 1) AND ((status = 2)))

所以它似乎完全忘记了原来的has_many通过友谊关系,然后不恰当地试图将User类用作连接表。

我做错了什么,或者这根本不可能?

4

4 回答 4

79

编辑:

Rails 3.1 支持嵌套关联。例如:

has_many :tasks
has_many :assigments, :through => :tasks
has_many :users, :through => :assignments

不需要下面给出的解决方案。有关更多详细信息,请参阅截屏视频。

原始答案

您正在传递一个has_many :through关联作为另一个has_many :through 关联的源。我不认为它会起作用。

  has_many :friends, 
           :through => :friendships,
           :conditions => "status = #{Friendship::FULL}"
  has_many :friends_comments, :through => :friends, :source => :comments

您可以通过三种方法来解决此问题。

1)编写关联扩展

 has_many  :friends, 
           :through => :friendships,
           :conditions => "status = #{Friendship::FULL}" do
     def comments(reload=false)
       @comments = nil if reload 
       @comments ||=Comment.find_all_by_user_id(map(&:id))
     end
 end

现在你可以得到朋友的评论如下:

user.friends.comments

2)在类中添加一个方法User

  def friends_comments(reload=false)
    @friends_comments = nil if reload 
    @friends_comments ||=Comment.find_all_by_user_id(self.friend_ids)
  end

现在你可以得到朋友的评论如下:

user.friends_comments

3)如果你想让它更有效率,那么:

  def friends_comments(reload=false)
    @friends_comments = nil if reload 
    @friends_comments ||=Comment.all( 
             :joins => "JOIN (SELECT friend_id AS user_id 
                              FROM   friendships 
                              WHERE  user_id = #{self.id}
                        ) AS friends ON comments.user_id = friends.user_id")
  end

现在你可以得到朋友的评论如下:

user.friends_comments

所有方法都会缓存结果。如果要重新加载结果,请执行以下操作:

user.friends_comments(true)
user.friends.comments(true)

或者更好:

user.friends_comments(:reload)
user.friends.comments(:reload)
于 2010-03-04T23:44:53.717 回答
8

有一个插件可以解决你的问题,看看这个博客

你安装插件

script/plugin install git://github.com/ianwhite/nested_has_many_through.git
于 2010-03-24T08:30:54.400 回答
5

虽然这在过去不起作用,但它现在在 Rails 3.1 中运行良好。

于 2011-12-01T19:17:48.077 回答
3

我发现这个博客条目很有用:http: //geoff.evason.name/2010/04/23/nested-has_many-through-in-rails-or-how-to-do-a-3-table-join /

于 2011-07-12T14:12:04.817 回答