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 關注
- 672 瀏覽
添加回答
舉報