class Property < ActiveRecord::Base
has_many :units
def self.default_scope
where('active_at <= :now AND inactive_at > :now', now: Time.zone.now)
end
end
class Unit < ActiveRecord::Base
belongs_to :property
def self.with_active_properties
joins(:property)
end
end
我正在尝试将 Rails 4.0.2 default_scope 与连接一起使用,但是当我调用时Unit.with_active_properties
为什么会得到以下重复的 SQL?
SELECT "units".* FROM "units" INNER JOIN "properties" ON "properties"."id" = "units"."property_id" AND (active_at <= '2014-03-11 03:36:13.994068' AND inactive_at > '2014-03-11 03:36:13.994068') AND (active_at <= '2014-03-11 03:36:13.967550' AND inactive_at > '2014-03-11 03:36:13.967550')