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