1
比方說,我有以下型號:如何編寫兩個碰撞連接的作用域?
# 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
如果我提出以下電話:
Video.in_collection_one.in_foo_collections
的ActiveRecord構建了SQL查詢抱怨做多加入後,我會得到一個錯誤 - 它將嘗試加入:collection_videos兩次(錯誤,應該只加入一次)和:收集一次(這是正確的)。這可能是由導軌中的錯誤引起的,但我想知道是否有解決方法。
注:我使用Rails/ActiveRecord的2.3.2版本
同樣的問題發生,因爲Rails延遲對include進行內部連接。我認爲它是一個Rails版本小於2.3.5的bug – 2011-01-10 16:33:04