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')
答案 0 :(得分:1)
经过进一步调查后,似乎这个错误是rails 4.0.x中的错误导致default_scope被调用两次的结果。我在github上发布了这个问题:
https://github.com/rails/rails/issues/14351
由于对这个问题提供了极好的帮助,所以需要得到拉里·里德的信用。