[英]Ruby-on-Rails: Multiple has_many :through possible?
Is it possible to have multiple has_many :through
relationships that pass through each other in Rails? 是否有可能有多个
has_many :through
在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. 我收到了这样的建议,作为我发布的另一个问题的解决方案,但一直无法让它工作。
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. 目标是为
friends_comments
创建一个has_many :through
,这样我就可以带一个User
并执行类似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
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:
当我尝试访问用户的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)))
When I just enter user.friends, which works, this is the query it executes: 当我输入有效的user.friends时,这是它执行的查询:
: SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users".id = "friendships".friend_id WHERE (("friendships".user_id = 1) AND ((status = 2)))
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. 因此,它似乎完全忘记了原始的
has_many
通过友谊关系,然后不恰当地尝试将User类用作连接表。
Am I doing something wrong, or is this simply not possible? 我做错了什么,或者这根本不可能?
Edit: 编辑:
Rails 3.1 supports nested associations. Rails 3.1支持嵌套关联。 Eg:
例如:
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.
有关详细信息,请参阅此截屏视频。
Original Answer 原始答案
You are passing a has_many :through
association as a source for another has_many :through
association. 您正在传递
has_many :through
关联作为另一个has_many :through
来源的关联。 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) Write an association extension 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) Add a method to the User
class. 2)向
User
类添加方法。
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) If you want this to be even more efficient then: 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
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)
OR better still: 或者更好的是:
user.friends_comments(:reload)
user.friends.comments(:reload)
虽然这在过去不起作用,但它现在在Rails 3.1中运行良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.