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

朋友是一个通过连接表的cyclic association.我可以用user.friends_comments条来创建一个简单的查询,比如说我可以用friends_comments条来创建一个has_many :through条 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:
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:
: SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users".id = "friendships".friend_id WHERE (("friendships".user_id = 1) AND ((status = 2)))

因此,它似乎完全忘记了最初的has_many-through-friendly关系,然后不恰当地try 将User类用作连接表.

是我做错了什么,还是这根本不可能?

推荐答案

Edit:

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

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

不需要下面给出的解决方案.有关更多详细信息,请参阅this屏幕广播.

Original Answer

你正在通过一个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

现在你可以得到朋友们的 comments 如下:

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

现在你可以得到朋友们的 comments 如下:

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

现在你可以得到朋友们的 comments 如下:

user.friends_comments

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

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

或者更好:

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

Ruby-on-rails相关问答推荐

用至少一个缺省值初始化Ruby数组的最简洁方法是什么?

为 has_attached_file 等活动记录属性创建类方法

Ruby on Rails 7 与 React 集成

Rails 7:加载所有刺激控制器

Gem::Ext::BuildError:ERROR:无法在macOS Monterey上构建Gem本机扩展

Rails - 如何从 http://example.com 重定向到 https://www.example.com

Rails 上的 build 和 new 有什么区别?

理解 Gemfile.lock:删除 Gemfile.lock 然后再次运行 bundle install 可以吗?

如何在终端中查看 Heroku 应用程序的当前数据库架构?

Rails 模型:您将如何创建一组预定义的属性?

Rails ActiveRecord 查询日期范围

Ruby 元素是否存在于数组中

Rails 3:验证组合值

是否可以在 Rails 集成测试或规范中指定用户代理?

form_tag 是否与 Simple_form 一起使用?

Elastic Beanstalk Ruby/Rails 需要安装 git,所以 bundle install 可以工作.. 但不是

Ruby on Rails 中的MySQL 服务器已经消失

加载常量时自动加载常量时检测到循环依赖

您推荐哪些工具来分析 Rails 应用程序?

Rails 3 设计,current_user 在模型中不可访问?