What explains this bizarre behavior? work_state
is an state attribute derived from aasm
gem, but I don't have trouble querying this with other models...
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
As requested, this is the state set up
aasm(:work, column: "work_state",no_direct_assignment: true ) do
state :pending_work, initial: true
state :in_progress
state :complete
state :not_fixable
What's further odd is that queries work with OTHER states, i.e., this works:
MaintenanceOrder.where(work_state:"in_progress").size
=> 12
Also the SQL looks fine when I do 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
Update on oddities.. is there a giant gotcha here that I don't know??? Another model has a SIMILAR issue. FWIW the states with issues, "pending_work"
above and "pending_start"
below both are the beginning, initial default states...
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