Rails覆盖范围选项

时间:2013-11-05 09:50:19

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

更像是重写命令

Entity.associations.order("field ASC").reorder("other_field DESC") 
# => result: GET ... order by other_filed DESC

是否可以覆盖范围选项?

Entity.associations.where("field = 1").where("field = 2")
# => GET ... where "field" = 1 and "field" = 2

# Desirable:
Entity.associations.where("field = 1").rescope(where("field = 2"))
# => GET ... where "field" = 2

P.S。
Rails 3

P.S.S 接受的答案对Rails4

有效

1 个答案:

答案 0 :(得分:2)

您可以使用unscope方法完全删除您所在的条件:

Entity.asscocations.where(field: 1).unscope(:where).where(field: 2)

或者只是从您的条件中删除特定字段:

Entity.asscocations.where(field: 1).unscope(where: :field).where(field: 2)