如何在 Ruby 中使用 "and" 和 "or" 条件编写 Active Record
How to write Active Record with "and" and "or" conditions in Ruby
我想用三个 and
条件(必须)和两个 or
条件编写 Active Record。
我写了这个:
@properties = Property.where(category_id: 2).
where(status: 3).
where(p_type: 'sell')`
这列出了具有 and
个条件的查询。
我需要在最后包含两个 or
条件。
它尝试了这个但出现错误:
@properties = Property.where(category_id: 2).
where(status: 3).
where(p_type: 'sell').
or(location: @location).
or(featured: '1)`
我该怎么做?
@properties=Property.where("(category_id = ? AND status = ? ) OR (location = ? OR featured = ?)", "2", "3", @location, "1")
你可以试试这个。
@properties = Property.where(category_id: 2, status: 3).where("(p_type = ? OR location = ? OR featured = ?)", 'sell', @location, '1')
我想用三个 and
条件(必须)和两个 or
条件编写 Active Record。
我写了这个:
@properties = Property.where(category_id: 2).
where(status: 3).
where(p_type: 'sell')`
这列出了具有 and
个条件的查询。
我需要在最后包含两个 or
条件。
它尝试了这个但出现错误:
@properties = Property.where(category_id: 2).
where(status: 3).
where(p_type: 'sell').
or(location: @location).
or(featured: '1)`
我该怎么做?
@properties=Property.where("(category_id = ? AND status = ? ) OR (location = ? OR featured = ?)", "2", "3", @location, "1")
你可以试试这个。
@properties = Property.where(category_id: 2, status: 3).where("(p_type = ? OR location = ? OR featured = ?)", 'sell', @location, '1')