Use a function in a conditions hash

Posted by Pierre on Stack Overflow See other posts from Stack Overflow or by Pierre
Published on 2011-01-15T07:38:46Z Indexed on 2011/01/15 7:53 UTC
Read the original article Hit count: 183

Filed under:

Hi,

I'm building a conditions hash to run a query but I'm having a problem with one specific case: conditions2 = ['extract(year from signature_date) = ?', params[:year].to_i] unless params[:year].blank?

conditions[:country_id] = COUNTRIES.select{|c| c.geography_id == params[:geographies]} unless params[:geographies].blank?
conditions[:category_id] = CATEGORY_CHILDREN[params[:categories].to_i] unless params[:categories].blank?
conditions[:country_id] = params[:countries] unless params[:countries].blank?
conditions['extract(year from signature_date)'] = params[:year].to_i unless params[:year].blank?

But the last line breaks everything, as it gets interpreted as follows:

AND ("negotiations"."extract(year from signature_date)" = 2010

Is there a way to avoid that "negotiations"." is prepended to my condition?

thank you, P.

© Stack Overflow or respective owner

Related posts about ruby-on-rails