Search code examples
sqlruby-on-railsrubylogicruby-on-rails-plugins

Putting in precedence in the where clause by using ez_where plugin for ruby on rails.


I am working on a project which used this plugin. I really can't use any other plugin to do what I have to do.

I am using this to build a WHERE clause in my query, which is otherwise built. Here's how I am doing it:

cond = EZ::Where::Condition.new
if first_condition
  cond.append "A > B"
end
if second_condition
  cond.append "C < D"
end
if third_condition
  cond.or "E > F"
  cond.or "G > H"
end

The issue is that any combitions of the first, second or third conditions can be true. But in every case, my query is supposed to be like this:

Where A > B AND C < D AND (E > F OR G > H)

And if Only first and third condition is true then:

Where A > B AND (E > F OR G > H)

How do I do it in ez_where plugin for ruby on rails? Or if not that, how do I do that at all?


Solution

  • cond = EZ::Where::Condition.new
    if first_condition
      cond.append "A > B"
    end
    if second_condition
      cond.append "C < D"
    end
    if third_condition
      cond.or "E > F"
      cond.or "G > H"
    end
    

    I just realized a really easy way of doing the above would be

    cond1 = EZ::Where::Condition.new
    if first_condition
      cond1.append "A > B"
    end
    if second_condition
      cond1.append "C < D"
    end
    cond2 = EZ::Where::Condition.new
    if third_condition
      cond2.or "E > F"
      cond2.or "G > H"
    end
    cond = EZ::Where::Condition.new
    cond.append cond