Ruby on rails 为什么enum总是在where子句中返回奇怪的响应?
我有一个模型-Ruby on rails 为什么enum总是在where子句中返回奇怪的响应?,ruby-on-rails,ruby-on-rails-4,enums,rails-activerecord,Ruby On Rails,Ruby On Rails 4,Enums,Rails Activerecord,我有一个模型-活动,看起来像这样: # == Schema Information # # Table name: activities # # id :integer not null, primary key # trackable_id :integer # trackable_type :string(255) # owner_id :integer # owner_type :string(255) # ke
活动
,看起来像这样:
# == Schema Information
#
# Table name: activities
#
# id :integer not null, primary key
# trackable_id :integer
# trackable_type :string(255)
# owner_id :integer
# owner_type :string(255)
# key :string(255)
# parameters :text
# recipient_id :integer
# recipient_type :string(255)
# created_at :datetime
# updated_at :datetime
# read_status :integer default(0)
#
class Activity < PublicActivity::Activity
enum read_status: [ :unread, :read ]
attr_accessible :read_status
end
但当我在控制台中执行此操作时,我会返回相同数量的记录:
[13] pry(main)> Activity.where(read_status: "read").count
(0.5ms) SELECT COUNT(*) FROM "activities" WHERE "activities"."read_status" = 0
=> 12
[14] pry(main)> Activity.where(read_status: "unread").count
(0.4ms) SELECT COUNT(*) FROM "activities" WHERE "activities"."read_status" = 0
=> 12
请注意,两个where
调用都在检查“read_status”=0
,尽管情况显然不是这样
在这种情况下,第一个应该检查read\u status=1
这是我第二次在enum中遇到此问题。访问活动状态的正确方法是使用
Activity.read
Activity.unread
您可以查看文档。我正在查找,但找不到。谢谢我知道一定有更简单的方法!顺便说一下,该文档不包括像您的示例那样返回集合的类方法。我知道我查阅了所有的文档,却找不到一个例子。但是你的建议非常有效。
Activity.read
Activity.unread