Question

I'm working with rails and I have this sql query I would like to do with arel.

"SELECT `people`.* FROM `peoples`  WHERE `peoples`.`home_id` = 289 AND (`people`.`created_at` BETWEEN '2013-03-12' AND '2013-03-15' AND (`people`.`first_name` LIKE '%jane%' OR `people`.`email` LIKE '%jane%'))"

Now, I have solved the query with arel but i would like to avoid repeating the "matches("%#{search}%")" for attributes that must match the same search variable.

a.where(a.arel_table[:created_at].in(date_range).and(a.arel_table[:first_name].matches("%#{search}%").or(a.arel_table[:email].matches("%#{search}%"))))

Something like:

a.where(a.arel_table[:created_at].in(date_range).and([:email, :first_name].each {|attr| or.(a.arel_table[:attr].matches("%#{search}%"))))

But this is not working.

Was it helpful?

Solution

The problem was that the condition looks like (((c1 or c2) or c3) or c4) but it seem to be that Arel's conception of "or" is binary.

So it's no possible to define them as (c1 or c2 or c3 or c4).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top