的Ruby-on-Rails的:多的has_many:通过可能吗? [英] Ruby-on-Rails: Multiple has_many :through possible?

查看:269
本文介绍了的Ruby-on-Rails的:多的has_many:通过可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能有多个的has_many:通过穿过Rails的相互关系?我收到的建议,作为另一个问题,我发布了一个解决方案,这样做,但一直无法得到它的工作。

Is it possible to have multiple has_many :through relationships that pass through each other in Rails? I received the suggestion to do so as a solution for another question I posted, but have been unable to get it to work.

朋友是通过一个连接表的循环关联。我们的目标是创建的has_many:通过对friends_comments,这样我就可以拿一个用户,并完成类似user.friends_comments得到由他的朋友在一个查询中提出的所有意见

Friends are a cyclic association through a join table. The goal is to create a has_many :through for friends_comments, so I can take a User and do something like user.friends_comments to get all comments made by his friends in a single query.

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错误:结果
错误:列users.user_id不存在结果
:SELECT意见* FROM意见INNER JOIN用户ON意见.user_id =用户.ID WHERE((用户.user_id = 1)和((状态= 2)))

This looks great, and makes sense, but isn't working for me. This is the error I'm getting in relevant part when I try to access a user's 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,它的工作原理,这是它执行查询:结果
:选择用户*从用户INNER JOIN友谊ON用户.ID =友谊.friend_id WHERE((友谊.user_id = 1)和((状态= 2)))

When I just enter user.friends, which works, this is the query it executes:
: SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users".id = "friendships".friend_id WHERE (("friendships".user_id = 1) AND ((status = 2)))

所以,好像它完全忘了如何通过友好的关系原来的has_many,然后不当尝试使用User类的连接表。

So it seems like it's entirely forgetting about the original has_many through friendship relationship, and then is inappropriately trying to use the User class as a join table.

我做得不对,或者是这根本不可能?

Am I doing something wrong, or is this simply not possible?

推荐答案

编辑:

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

Rails 3.1 supports nested associations. E.g:

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

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

There is no need for the solution given below. Refer to this screencast for more details.

原来的答案

您是通过一个的has_many:通过协会为另一个的has_many源:通过
协会。我不认为这会工作。

You are passing a has_many :through association as a source for another has_many :through association. I don't think it will work.

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

您有三种方法来解决这个问题。

You have three approaches to solving this issue.

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

现在,你可以得到朋友们的意见如下:

Now you can get the friends comments as follows:

user.friends.comments

2)添加一个方法到用户类。

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

现在,你可以得到朋友们的意见如下:

Now you can get the friends comments as follows:

user.friends_comments

3)如果你希望这是更有效的话:

3) If you want this to be even more efficient then:

  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

现在,你可以得到朋友们的意见如下:

Now you can get the friends comments as follows:

user.friends_comments

的所有方法缓存结果。如果你想重新加载结果做到以下几点:

All methods cache the results. If you want to reload the results do the following:

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

或者更好的是:

user.friends_comments(:reload)
user.friends.comments(:reload)

这篇关于的Ruby-on-Rails的:多的has_many:通过可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆