自我引用has_many,:through

时间:2011-05-05 05:34:59

标签: mysql ruby-on-rails has-many-through belongs-to

这是我的用户模型:

class User < ActiveRecord::Base

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

end

这是我的友谊模型:

class Friendship < ActiveRecord::Base

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

  set_table_name :users_users
end

现在,我在User模型中有一个名为* is_awesome *。

的布尔属性

当我尝试运行此查询时:

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))

知道发生了什么事吗?

1 个答案:

答案 0 :(得分:0)

你必须改变:condition来引用用户表,它应该是这样的:

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