繁体   English   中英

自引用 has_many, :through

[英]Self-referencing has_many, :through

这是我的用户 model:

class User < ActiveRecord::Base

  has_many :friends, :class_name => 'Friendship', :dependent => :destroy

end

这是我的友谊model:

class Friendship < ActiveRecord::Base

  belongs_to :user
  belongs_to :friend, :class_name => 'User', :foreign_key => 'friend_id'

  set_table_name :users_users
end

现在,我在名为 *is_awesome* 的用户 model 中有一个 boolean 属性。

当我尝试运行此查询时:

User.find(1).friends.find(:all, :include => :users, :conditions => {:is_awesome => false})

我收到以下错误:

ActiveRecord::StatementInvalid: Mysql::Error: 
Unknown column 'users_users.is_awesome' in 'where clause': 
SELECT * FROM `users_users` 
WHERE (`users_users`.user_id = 1 AND (`users_users`.`is_awesome` = 0))

知道发生了什么吗?

您必须更改:条件以引用用户表,它应该看起来像这样:

User.find(1).friends.find(:all, :include => :users, :conditions => "users.is_awesome IS FALSE")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM