我正在努力从Rails 3升级到Rails 4.我有一个范围定义似乎正在放弃where子句和其他应用范围。
我有这些模特:
class GoalWeightSet < ActiveRecord::Base
has_many :goal_weight_set_nutrients, -> {order(:nutrient_id).includes(:nutrient)}, :inverse_of=>:goal_weight_set, :dependent=>:destroy, :autosave=>true
end
class GoalWeightSetNutrient < ActiveRecord::Base
belongs_to :goal_weight_set, :inverse_of=>:goal_weight_set_nutrients
scope :model_weights, -> {where(:index=>false)}
scope :model, -> {where(:index => false)}
end
我认为在Rails 4中使用这两个范围之间存在完全不同的行为
# this is broken, it should have a where clause
GoalWeightSet.first.goal_weight_set_nutrients.model.count
(2.8ms) SELECT COUNT(*) FROM "goal_weight_set_nutrients"
=> 37288
# this is what I expect
GoalWeightSet.first.goal_weight_set_nutrients.model_weights.count
(0.4ms) SELECT COUNT(*) FROM "goal_weight_set_nutrients" WHERE "goal_weight_set_nutrients"."goal_weight_set_id" = $1 AND "goal_weight_set_nutrients"."index" = 'f' [["goal_weight_set_id", 8]]
=> 31
scope :model
在Rails 3中工作正常。为什么这个范围试图加载所有对象?名为&#39; model&#39;?
以下是您可以运行的要点,显示错误:https://gist.github.com/johnnaegle/9647941
答案 0 :(得分:1)
据我所知,关联上的“模型”意味着Rails 4中的不同之处:
Klass.where("1=0").model
=> Klass(id: integer)
如此有效,您不能将模型用作范围名称。