Search code examples
ruby-on-railsactiverecordwildcarddefault-scope

% not working as wildcard for ActiveRecord default_scope query?


I am using ActiveRecord in my Rails project and one of my classes looks like this:

class ServerModel < ActiveRecord::Base

  set_table_name "S985_947_MODELS_VW"
  set_primary_key "model_barcode"

  default_scope :conditions => ["FULLNAME like '\/IT INFRASTRUCTURE\/HARDWARE\/SERVER\/PHYSICAL\/%' OR FULLNAME like '\/IT INFRASTRUCTURE\/HARDWARE\/SERVER\/PHYSICAL\/%'"]

  acts_as_reportable
  acts_as_entity
end

My default_scope method returns a malformed format string - %' error. I think it is the % wildcard that causes all the trouble. I also tried without escaping the /, so that's not it...

Could you help me?

Thanks


Solution

  • Change it to:

    default_scope :conditions => ["FULLNAME like ? OR FULLNAME like ?", '\/IT INFRASTRUCTURE\/HARDWARE\/SERVER\/PHYSICAL\/%', '\/IT INFRASTRUCTURE\/HARDWARE\/SERVER\/PHYSICAL\/%']