问题是:
class MyModel < ActiveRecord::Base
enum status: [:queued, :pending, :processed]
scope :queued_and_pending, -> { where(status: [statuses[:queued], statuses[:pending]]) }
end
MyModel.queued_and_pending.new
# => ArgumentError: '[0, 1]' is not a valid status
现在我知道做这件事的正确方法是MyModel.new而不是MyModel.queued_and_pending.new。但我的问题是,它为什么会引发这个错误?显然,我希望在我的作用域中按多个状态进行过滤。
发布于 2018-08-22 04:14:36
,但在最坏的情况下,会出现类似"new is not defined on ActiveRecord::Relation“的错误
实例化来自关系/查询的记录非常有用。这与您在以下情况中使用的机制相同:
@comment = @user.comments.build # comment gets user_id pre-populated
在这种情况下,ActiveRecord理所当然地未能找到将查询转换为有效对象状态的方法。尝试使用只有一种状态的作用域,它会工作。
https://stackoverflow.com/questions/51955834
复制相似问题