在边缘API(http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html)中,我们可以使用
Conversation.where(status: [:active, :archived])
Conversation.where.not(status: :active)
但现在我们只能使用像
这样的东西Conversation.where(status: Conversation.statues[:active])
然后,在Rails 4.1.8中,如何同时查询:active
和:archived
?
答案 0 :(得分:1)
这是你想要的:
Conversation.where(status: Conversation.statues.values_at(:active, :archived))
.. The mappings通过带有复数属性名称的类方法公开,该方法返回HashWithIndifferentAccess
中的映射。
演示:
[12] pry(main)> Pet.all
Pet Load (0.5ms) SELECT "pets".* FROM "pets"
=> [#<Pet id: 6, animals: 1, created_at: "2015-02-21 10:40:19", updated_at: "2015-02-21 10:40:19", name: "Tilly">,
#<Pet id: 7, animals: 0, created_at: "2015-02-21 10:40:31", updated_at: "2015-02-21 10:40:54", name: "Xav">,
#<Pet id: 5, animals: 1, created_at: "2015-02-19 18:27:28", updated_at: "2015-02-21 10:41:06", name: "Mua">]
[13] pry(main)> Pet.where(animals: Pet.animals.values_at(:dog))
Pet Load (0.7ms) SELECT "pets".* FROM "pets" WHERE "pets"."animals" IN (0)
=> [#<Pet id: 7, animals: 0, created_at: "2015-02-21 10:40:31", updated_at: "2015-02-21 10:40:54", name: "Xav">]
[14] pry(main)> Pet.where(animals: Pet.animals.values_at(:cat))
Pet Load (0.8ms) SELECT "pets".* FROM "pets" WHERE "pets"."animals" IN (1)
=> [#<Pet id: 6, animals: 1, created_at: "2015-02-21 10:40:19", updated_at: "2015-02-21 10:40:19", name: "Tilly">,
#<Pet id: 5, animals: 1, created_at: "2015-02-19 18:27:28", updated_at: "2015-02-21 10:41:06", name: "Mua">]
但下面更清洁: -
Conversation.where(status: [:active, :archived])