Ruby-on-Rails:多个has_many:通过可能吗?

Wil*_*nes 54 ruby-on-rails associations has-many-through

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

好友是通过联接表的循环关联.我的目标是创建一个has_many :throughfor friends_comments,所以我可以采取行动,Useruser.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
Run Code Online (Sandbox Code Playgroud)

这看起来很棒,而且很有意义,但不适合我.当我尝试访问用户的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类用作连接表.

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

Har*_*tty 76

编辑:

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

has_many :tasks
has_many :assigments, :through => :tasks
has_many :users, :through => :assignments
Run Code Online (Sandbox Code Playgroud)

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

原始答案

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

  has_many :friends, 
           :through => :friendships,
           :conditions => "status = #{Friendship::FULL}"
  has_many :friends_comments, :through => :friends, :source => :comments
Run Code Online (Sandbox Code Playgroud)

您有三种方法可以解决此问题.

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
Run Code Online (Sandbox Code Playgroud)

现在您可以获得以下朋友评论:

user.friends.comments
Run Code Online (Sandbox Code Playgroud)

2)向User类添加方法.

  def friends_comments(reload=false)
    @friends_comments = nil if reload 
    @friends_comments ||=Comment.find_all_by_user_id(self.friend_ids)
  end
Run Code Online (Sandbox Code Playgroud)

现在您可以获得以下朋友评论:

user.friends_comments
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

现在您可以获得以下朋友评论:

user.friends_comments
Run Code Online (Sandbox Code Playgroud)

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

user.friends_comments(true)
user.friends.comments(true)
Run Code Online (Sandbox Code Playgroud)

或者更好的是:

user.friends_comments(:reload)
user.friends.comments(:reload)
Run Code Online (Sandbox Code Playgroud)


Jar*_*arl 8

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

你安装插件

script/plugin install git://github.com/ianwhite/nested_has_many_through.git
Run Code Online (Sandbox Code Playgroud)


小智 5

尽管这在过去不起作用,但现在在Rails 3.1中可以正常工作。