Concatenate (glue) where conditions by OR or AND (Arel, Rails3)

眉间皱痕 提交于 2019-12-03 15:38:32

are you looking for the form:

users.where(users[:name].eq('bob').or(users[:age].lt(25)))

docs: https://github.com/rails/arel

users.where(users[:name].eq('bob').or(users[:age].lt(25))) is close, but you need to get the arel_table to specify the columns, e.g.

t = User.arel_table
User.where(t[:name].eq('bob').or(t[:age].lt(25)))

I know that for AND concatenation you can do:

users = User.where(:name => 'jack')
users = users.where(:job => 'developer')

and you get a concatenation with AND in SQL (try it with #to_sql at the end)

Other than that you can do:

where1=table.where(...)
where2=table.where(...)
where1 & where2

example:

(User.where(:name => 'jack') & User.where(:job => 'dev')).to_sql
=> "SELECT `users`.* FROM `users` WHERE `users`.`name` = 'jack' AND `users`.`job` = 'dev'" 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!