2011-01-06 7 views
1

のは、私は次のモデルがあるとしましょう:どのようにして、衝突する結合を持つ2つのスコープを記述できますか?

# video.rb 
class Video < ActiveRecord::Base 
    has_many :collection_videos, :dependent => :destroy 
    has_many :collections, :through => :collection_videos 
    named_scope :in_collection_one, 
       :joins => :collection_videos, 
       :conditions => "collection_videos.collection_id = 1" 

    named_scope :in_foo_collections, 
       :joins => :collections, 
       :conditions => "collections.foo = true" 
end 

# collections.rb 
class Collection < ActiveRecord::Base 
    has_many :videos, :through => :collection_videos 
    has_many :collection_videos, :dependent => :destroy 
end 

# collection_videos.rb 
class CollectionVideos < ActiveRecord::Base 
    belongs_to :collection 
    belongs_to :video 
end 

私は次の呼び出しにする場合:ActiveRecordのは、参加する複数のをやって文句SQLクエリを構築した後、私はエラーになります

Video.in_collection_one.in_foo_collections 

を - それをcollection_videosに2回参加します(間違っていて1回だけ参加する必要があります)。:コレクションは1回(これは正しい)。これはおそらくレールのバグが原因ですが、周りに道があるかどうか疑問に思っています。

注:私は2.3.2

答えて

0

はあなたが:include代わりの:joinを使用することができレール/ ActiveRecordのバージョンを使用していますか?ジョインを行うために:includeを使用している人の例については、this questionを参照してください。

+1

Railsが:includeで内部結合を行うことを擁護するのと同じ問題が発生します。私はバージョンが2.3.5未満のRailsのバグだと思う –

関連する問題