Wednesday, 15 April 2015

Ruby appending methods to constant dynamically - Need better way to do this -



Ruby appending methods to constant dynamically - Need better way to do this -

i doing this

@orders = order.unscoped.dated @orders = @orders.search_for(params["search"]) if params["search"].present? @orders = @orders.where(:items_received_status => true) if params[:air].present? && params[:air] == 't' @orders = @orders.tagged_with(params[:tags]) if params[:tags].present?

is there improve way write same code

something like

@orders = order self.where(some condition) self.joins(some table) end

there should way avoid repeating assignment operator

i think simplest way create model methods this:

class order def self.air(air) homecoming self.where(items_received_status: true) if air == 't' self end def search(search...

so can chain in controller this:

order.unscoped.dated.air(params[:air]).seach(...

ruby-on-rails ruby

No comments:

Post a Comment