简体   繁体   中英

% 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

Change it to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM