1

I would like to apply logical delete in my application(Instead of permanently deleting a record just have been marked as deleted). I have Added available column to all tables with default value true. Now I want common place to write the following code for all models.

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.

With reference Rails extending ActiveRecord::Base, I decided to use monkey patch to do above. Created monkey patch file in config/initializer/active_record_patch.rb:

class ActiveRecord::Base
   def inactive
      update_attribute(:available, false)
   end

   default_scope :available => true
end

Getting following error when add the default_scope

/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/base.rb:1212:in class_of_active_record_descendant': undefined methodabstract_class?' for Object:Class (NoMethodError)

Community
  • 1
  • 1

2 Answers2

1

Try default_scope where(:available => true).

Colin R
  • 17,711
  • 2
  • 20
  • 28
  • Can you give any information about the error you got? For what it's worth, the ActiveRecord docs say you should format as I showed (check the `ActiveRecord::Scoping::Default::ClassMethods` docs). – Colin R Jul 26 '12 at 13:59
0

I believe monkeypatching ActiveRecord::Base is not the way to go. Maybe you should try to create a module that when included/extended in your models, create this functionalities seamlessly.

MurifoX
  • 14,991
  • 3
  • 36
  • 60