我有一个模特
class Post < ApplicationRecord
enum status: { pending: 'pending',accepted: 'accepted',cancelled: 'cancelled' }
end
在数据库中,我在数据库中的记录很少,当我进行group_by或计数时,我得到如下结果
Jayesh@rails:~/yashu$ rails c
Loading development environment (Rails 5.1.6.1)
[1] pry(main)> Post.group(:status).count
(0.6ms) SELECT COUNT(*) AS count_all,"posts"."status" AS posts_status FROM "posts" GROUP BY "posts"."status"
=> {"pending"=>5,"cancelled"=>1,"accepted"=>4}
现在如果我在模型中进行更新,则具有相同的数据集
class Post < ApplicationRecord
enum status: { pending: 'pending',accepted: 'accepted' }
end
再做一次控制台查询,这三个查询给我下面的结果
[1] pry(main)> Post.group(:status).count
(0.9ms) SELECT COUNT(*) AS count_all,nil=>1,"accepted"=>4}
[2] pry(main)> Post.where(status: nil).count
(1.3ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."status" IS NULL
=> 0
[3] pry(main)> Post.where(status: nil)
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."status" IS NULL
=> []
如果我找到该记录,就会看到如下图
[7] pry(main)> Post.find(8).status
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2 [["id",8],["LIMIT",1]]
=> nil
因此,我如何获取设置为nil的记录(如果确实如此),实际上是由于不同的开发人员在同一模型上工作,并向其枚举哈希添加不同的键并破坏了代码而引起的视图文件sample.html.erb post.status 中的某个时间点未显示(对于那些其键现在不在枚举哈希中,但记录在数据库中创建过一次的键)。