我知道有三个主要的符号为whereActiveRecord方法提供参数:
指定and的where方法是直截了当:
# Pure String notation
Person.where("name = 'Neil' AND age = 27")
# Array notation
Person.where(["name = ? AND age = ?", 'Neil', 27])
# Hash notation
Person.where({name: "Neil", age: 27})
Run Code Online (Sandbox Code Playgroud)
or为这个相同的where方法指定是为了哈希语法.可能吗?
# Pure String notation
Person.where("name = 'Neil' OR age = 27")
# Array notation
Person.where(["name = ? OR age = ?", 'Neil', 27])
# Hash notation DOESN'T WORK
Person.where({name: "Neil" OR age: 27})
Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord ruby-on-rails-5
我正在尝试合并两个范围或向现有范围添加更多内容。
scope :public_bids, -> {
where(status: [Status::ACCEPTED, Status::OUTBID, Status::SOLD, Status::NO_SALE],
bid_type: [BidType::MANUAL, BidType::AUTO, BidType::LIVE])
scope :outbid_maxbids, -> {
where(status: Status::OUTBID, bid_type: BidType::MAXBID)
Run Code Online (Sandbox Code Playgroud)
我无法确定如何将OR它们组合在一起或合并到一个范围中。有什么建议/指导吗?我更希望将它们合并到一个范围中。
我有一个范围:
includes(:countries).where("profiles.sector = :sector OR advices.sector = :sector", :sector => sector)
Run Code Online (Sandbox Code Playgroud)
它产生以下SQL:
SELECT `profiles`.* FROM `profiles` INNER JOIN `advices` ON `advices`.`profile_id` = `profiles`.`id` WHERE (profiles.sector = 'Forestry_paper' OR advices.sector = 'Forestry_paper')
Run Code Online (Sandbox Code Playgroud)
(是的,我Profile和我的Country模特中都有国家)
不幸的是,OR似乎失败了:
它不会使配置文件只有适当的扇区,但没有相关的建议.思考?