4 回答

TA贡献1841条经验 获得超3个赞
你会做
Person.where('name=? OR lastname=?', 'John', 'Smith')
目前,新的AR3语法不提供任何其他OR支持(即不使用某些3rd party gem)。

TA贡献1752条经验 获得超4个赞
根据此拉取请求,Rails 5现在支持以下用于链接查询的语法:
Post.where(id: 1).or(Post.where(id: 2))
通过这个gem还将功能移植到Rails 4.2中。

TA贡献1811条经验 获得超6个赞
使用ARel
t = Person.arel_table
results = Person.where(
t[:name].eq("John").
or(t[:lastname].eq("Smith"))
)

TA贡献1797条经验 获得超6个赞
Rails4的更新
不需要第三方宝石
a = Person.where(name: "John") # or any scope
b = Person.where(lastname: "Smith") # or any scope
Person.where([a, b].map{|s| s.arel.constraints.reduce(:and) }.reduce(:or))\
.tap {|sc| sc.bind_values = [a, b].map(&:bind_values) }
旧答案
不需要第三方宝石
Person.where(
Person.where(:name => "John").where(:lastname => "Smith")
.where_values.reduce(:or)
)
- 4 回答
- 0 关注
- 635 浏览
添加回答
举报