Wednesday 15 June 2011

ruby - add superior statment in activerecord hash condition -



ruby - add superior statment in activerecord hash condition -

i have hash witch contains many conditions want add together status wich utilize superior statment how can exmple

// conditions hash of conditions conditions[:id]="> 100" personels=personel.find(:all, :conditions=>conditions)

usually (not sure if understood goal) store multiple conditions in array add together more conditions using arrays << append operator, like:

conditions array: conditions = [] conditions << "id > '100'" conditions << "name '%#{params[:name]}%'" # # ... more conditions here # conditions = conditions.join(' , ') personel.where(conditions) conditions hash: conditions = {} conditions[:id] = "> '100'" conditions[:name] = "like '%#{params[:name]}%'" # # ... more conditions here # conditions = conditions.map{|k,v| "#{k} #{v}"}.join(' , ') personel.where(conditions)

ruby activerecord

No comments:

Post a Comment