假设我有以下型号:
# 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两次(错误,应该只加入一次),和:collections一次(这是正确的)。这可能是因为轨道中的错误引起的,但我想知道是否有办法解决它。
注意:我使用的是Rails / ActiveRecord版本2.3.2