ruby-on-railsruby-on-rails-4ruby-2.2

moving rails2 to 4 and getting error - undefined method `with_scope'


How to replace this Query in rails4.1.9

   AuditArea.send(query_options[:include_retired] ? :with_exclusive_scope :  :with_scope) {

     # some stuff
  }

Getting Error undefined method `with_scope' .


Solution

  • The with_scope is now called scoping in the newer Rails versions. with_exclusive_scope should now be unscoped. Both methods accept a block so your code should work OK with them.

    See docs for scoping and unscoped for more info.

    Update: the scoping method does not work if called on the class itself. It has to be called already on a scope (as opposed to unscoped which works on a bare model class). I would first add the "harmless" scope all (which selects all records and thus behaves the same way as the bare model class AuditArea) to the select so that both variants of the send work:

    AuditArea.all.send(query_options[:include_retired] ? :unscoped : :scoping) {
      # ...
    }