ActiveRecord,其中查询不适用于属性但 select 方法是
ActiveRecord where query not working for attributes but select method is
如何解释这种奇怪的行为? work_state
是从 aasm
gem 派生的状态属性,但我用其他模型查询它没有问题...
MaintenanceOrder.all.select { |m| m.work_state == "pending_work" }.size
=> 235
MaintenanceOrder.all.pluck(:work_state).select { |s| s == "pending_work" }.size
=> 235
MaintenanceOrder.last.work_state
=> "pending_work"
# so at this point... obviously there are MaintenanceOrders with work_state of "pending_work", and yet...
MaintenanceOrder.where(work_state:"pending_work").size
=> 0
应要求,这是设置的状态
aasm(:work, column: "work_state",no_direct_assignment: true ) do
state :pending_work, initial: true
state :in_progress
state :complete
state :not_fixable
更奇怪的是,查询适用于其他状态,即,这有效:
MaintenanceOrder.where(work_state:"in_progress").size
=> 12
另外 SQL 在我 log_level = :debug
时看起来不错
MaintenanceOrder.where(work_state:"pending_work").size
(15.9ms) SELECT COUNT(*) FROM "maintenance_orders" WHERE "maintenance_orders"."work_state" = 'pending_work'
=> 0
更新怪事..这里有我不知道的大问题吗???另一个模型有类似的问题。 FWIW 有问题的状态,上面的 "pending_work"
和下面的 "pending_start"
都是开始,初始默认状态...
Item.where(aasm_state: "pending_start").size
=> 19 # so at least some records are found, but not all of them
Item.all.pluck(:aasm_state).select { |s| s == "pending_start" }.size
=> 19
Item.all.select { |i| i.aasm_state == "pending_start"}.size
=> 94
# If I delve a little deeper.... it appears that the where query is just picking up the last few records... even though there's no size limit indicated?? in other words
where_ids = Item.where(aasm_state: "pending_start").map(&:id).sort
select_ids = Item.all.select { |i| i.aasm_state == "pending_start"}.map { |i| i.id }.sort
where_ids == select_ids.last(19)
=> true
好的,我解决了这个问题...但非常不满意,因为这对我来说只是一个普通的谜。对于上述两种不同的情况,解决方案是不同的。两者的根本原因似乎不同,但似乎在这两种情况下我都不知道原因...
MaintenanceOrder 模型问题
我在这里做了很多 git diffs
来检查我的理智。从字面上看...我将 config.log_level = :debug
转为 production.rb
然后将应用程序推向生产。我监测到查询中使用了正确的 SQL 语句(正如我复制到上述问题中一样)。然后我重置 config.log_level = :info
并推送到生产环境。然后突然就成功了。
没有其他变化..突然MaintenanceOrder.where(work_state:"pending_work").size
返回了正确的值
物品模型问题
在这里,where
查询找到了一些但不是所有记录,而且它找到的子集只是最近的,这让我想知道 maaaaaybe 旧记录......是......保存...以某种方式不正确?毕竟我刚刚做了一个大的数据库迁移。
所以我 运行 Item.all.select {|i| i.aasm_state == "pending_start"}.each { |i| i.update_column(:aasm_state,"pending_start") }
,这解决了问题
如何解释这种奇怪的行为? work_state
是从 aasm
gem 派生的状态属性,但我用其他模型查询它没有问题...
MaintenanceOrder.all.select { |m| m.work_state == "pending_work" }.size
=> 235
MaintenanceOrder.all.pluck(:work_state).select { |s| s == "pending_work" }.size
=> 235
MaintenanceOrder.last.work_state
=> "pending_work"
# so at this point... obviously there are MaintenanceOrders with work_state of "pending_work", and yet...
MaintenanceOrder.where(work_state:"pending_work").size
=> 0
应要求,这是设置的状态
aasm(:work, column: "work_state",no_direct_assignment: true ) do
state :pending_work, initial: true
state :in_progress
state :complete
state :not_fixable
更奇怪的是,查询适用于其他状态,即,这有效:
MaintenanceOrder.where(work_state:"in_progress").size
=> 12
另外 SQL 在我 log_level = :debug
MaintenanceOrder.where(work_state:"pending_work").size
(15.9ms) SELECT COUNT(*) FROM "maintenance_orders" WHERE "maintenance_orders"."work_state" = 'pending_work'
=> 0
更新怪事..这里有我不知道的大问题吗???另一个模型有类似的问题。 FWIW 有问题的状态,上面的 "pending_work"
和下面的 "pending_start"
都是开始,初始默认状态...
Item.where(aasm_state: "pending_start").size
=> 19 # so at least some records are found, but not all of them
Item.all.pluck(:aasm_state).select { |s| s == "pending_start" }.size
=> 19
Item.all.select { |i| i.aasm_state == "pending_start"}.size
=> 94
# If I delve a little deeper.... it appears that the where query is just picking up the last few records... even though there's no size limit indicated?? in other words
where_ids = Item.where(aasm_state: "pending_start").map(&:id).sort
select_ids = Item.all.select { |i| i.aasm_state == "pending_start"}.map { |i| i.id }.sort
where_ids == select_ids.last(19)
=> true
好的,我解决了这个问题...但非常不满意,因为这对我来说只是一个普通的谜。对于上述两种不同的情况,解决方案是不同的。两者的根本原因似乎不同,但似乎在这两种情况下我都不知道原因...
MaintenanceOrder 模型问题
我在这里做了很多 git diffs
来检查我的理智。从字面上看...我将 config.log_level = :debug
转为 production.rb
然后将应用程序推向生产。我监测到查询中使用了正确的 SQL 语句(正如我复制到上述问题中一样)。然后我重置 config.log_level = :info
并推送到生产环境。然后突然就成功了。
没有其他变化..突然MaintenanceOrder.where(work_state:"pending_work").size
返回了正确的值
物品模型问题
在这里,where
查询找到了一些但不是所有记录,而且它找到的子集只是最近的,这让我想知道 maaaaaybe 旧记录......是......保存...以某种方式不正确?毕竟我刚刚做了一个大的数据库迁移。
所以我 运行 Item.all.select {|i| i.aasm_state == "pending_start"}.each { |i| i.update_column(:aasm_state,"pending_start") }
,这解决了问题