我有一個模型用戶named_scope和HABTM協會
class User < ActiveRecord::Base
has_many :ratings
has_many :rated_films, :through => :ratings, :source => :film
end
和電影
class Film < ActiveRecord::Base
has_many :users, :through => :ratings
end
我希望找到那些尚未被評爲由指定用戶的所有影片,不便像
class Film < ActiveRecord::Base
has_many :users, :through => :ratings
named_scope :not_rated_by_user, lambda { |user|
{:joins => :users, :conditions => ['? NOT IN users', user]}
}
end
Film.not_rated_by_user(User.first)
我不熟悉SQL,所以我不太確定這是否可以在命名範圍內實現。
非常感謝
尤里
這是最初發布後很久,但有一個答案可能有所幫助:http://stackoverflow.com/questions/7032194/rails-habtm-and-finding-record-with-no-association – ScottJShea 2013-07-08 23:09:13