rails default_scope抛出异常:未定义的方法abstract_class?对于对象:类



我想在我的应用程序中应用逻辑删除(而不是永久删除刚刚标记为已删除的记录)。我已将可用列添加到所有默认值为true的表中。现在,我想要一个通用的地方为所有模型编写以下代码。

1) Write the instance method which make 'available' column value false when user clicks on destroy link.
2) Merge the 'available=true' condition to all ActiveRecord's queries while fetching the records.

参考Rails扩展ActiveRecord::Base,我决定用猴子补丁来做上面的事情。在config/iinitializer/active_record_patch.rb:中创建了monkey补丁文件

class ActiveRecord::Base
   def inactive
      update_attribute(:available, false)
   end
   default_scope :available => true
end

添加default_scope 时出现以下错误

/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/base.rb:1212:在class_of_active_record_descendant': undefined method抽象类中?'对于对象:类(NoMethodError)

尝试default_scope where(:available => true)

我认为猴痘ActiveRecord::Base不是一条路。也许你应该尝试创建一个模块,当它包含/扩展到你的模型中时,可以无缝地创建这些功能。

相关内容

最新更新