ruby - Rails 3 update scope with conditions, include, and args to Rails 4 -
i'm upgrading rails 3 code base rails 4 , have been updating use new scope style , querying interface.
i'm unsure how switch on scope using include conditions in scope's lambda.
scope :search_stuff, lambda { |search| search_conditions = array.new(4, "%#{search}%") { :include => {:sponsor => [], :routing_form_pi_users => [:user], :item => []}, :conditions => ["id ? or title ? or users.name ? or sponsors.name ?", *search_conditions] } }
when compare documentation query interface in different versions of rails guide can see interface eager loading of multiple associations didn't change much.
the example in rails 2.3.8 syntax:
category.find 1, :include => {:posts => [{:comments => :guest}, :tags]}
the example rails 4.2 syntax:
category.includes(articles: [{ comments: :guest }, :tags]).find(1)
therefore should possible copy old nested hash new syntax:
scope :search_stuff, lambda { |search| includes(item: [], routing_form_pi_users: [:user], sponsor: []). where('id ? or title ? or users.name ? or sponsors.name ?', *array.new(4, "%#{search}%")) }
Comments
Post a Comment