自引用 has_many, :through
这是我的用户模型:
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
现在,我在用户模型中有一个名为 *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))
知道发生了什么吗?
Here's my User model:
class User < ActiveRecord::Base
has_many :friends, :class_name => 'Friendship', :dependent => :destroy
end
Here's my Friendship model:
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => 'User', :foreign_key => 'friend_id'
set_table_name :users_users
end
Now, I have a boolean attribute in the User model called *is_awesome*.
When I try to run this query:
User.find(1).friends.find(:all, :include => :users, :conditions => {:is_awesome => false})
I get the following error:
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))
Any idea what's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须更改 :condition 来引用用户表,它应该看起来像这样:
You have to change :condition to refer to user table, it should looks something like this: