Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Squeel and rails... dynamic where clause

Using Squeel, in a rails app, I have a hash of conditions:

{'trans' => 'manual'}

which i eventually plan on moving into an array... so i can also have an operator assignment.

[[field,operator,value][field,operator,value]]

I want to use a Model method, which for now i omit the operator and am I just trying == to get this to work... however, what i have below does not work.

def self.with_conditions(conditions)
    joins{car}.where do
      conditions.map {|key,value| (key==value) }.inject(:&)
    end
end

I also tried this:

def self.with_conditions(conditions)
  joins{car}.where do
    query = nil

    conditions.each do |key, value|
      q = (key == value)

      if query
        query &= q
      else
        query = q
      end
    end

    query     
  end
end

So, how do i get this to work with the ==, and then how would i eventually get this to work with a dynamic operator as well? thanks

In console, my SQL doesn't read in any of my conditions... for example:

in console:

> Timeslip.with_conditions({'car.year'=>'1991'})

SELECT "timeslips".* FROM "timeslips" INNER JOIN "cars" ON "cars"."id" = "timeslips"."car_id"
like image 678
Joel Grannas Avatar asked Nov 28 '25 05:11

Joel Grannas


1 Answers

You need to programmatically build the Squeel query. For example:

def self.with_conditions(conditions)
  conditions.map do |col, str|
    Squeel::Nodes::Predicate.new(Squeel::Nodes::Stub.new(col), :matches, str) # (email.matches "[email protected]")
  end.inject do |t, expr|
    t & expr # joins each expression from the .map above with & - to be converted to AND in the sql
  end.tap do |block|
    return where{(block)} # pass the constructed expression to Squeel
  end
end

On my User::User model I can run

User::User.with_conditions({email: "[email protected]", first_name: "Deefour"}).to_sql

and I will get

SELECT "user_users".* FROM "user_users"  WHERE (("user_users"."email" LIKE '[email protected]' AND "user_users"."first_name" LIKE 'Deefour'))
like image 181
deefour Avatar answered Nov 30 '25 21:11

deefour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!