【发布时间】:2010-09-24 16:48:10
【问题描述】:
我的应用有以下模型:user 和 watch_list。 User 有属性 id、name,WatchList 有属性 user_id、friend_id。
class WatchList < ActiveRecord::Base
belongs_to :user
has_many :friends, :class_name => "User", :foreign_key => "friend_id"
end
class User < ActiveRecord::Base
has_one :watch_list
has_many :friends, :through => :watch_list
def friends_with(friend_id)
self.friends.each do |f|
if f.id == friend_id
return true
end
end
return false
end
end
由于某种原因,当我使用 @current_user.friends_with(friend_id) 时,我总是得到“真”作为响应。任何想法为什么这不起作用? (我知道@current_user 有效)
谢谢!
【问题讨论】:
标签: ruby-on-rails arrays activerecord associations