Code bug
NoMethodError
undefined method 'user_id' for nil user_id = audits.reorder(id: :desc).first.user_id ^^^^^^^^
In one line
claim_denied_by crashes when a denied billing item has no audit rows, and the code on releases/master still has no guard for that.
Checked against the code
What happened
- The serializer calls claim_denied_by for every item whose status is denied.
- That method takes the latest audit and reads user_id without checking that an audit exists.
- A denied item with no audits gives nil, so the index action returns a 500.
- There were 6 events for 5 users over 56 days. That fits a few bad records and not a path that always fails.
What to do next
- Code fix In app/models/member_billing_item.rb, change claim_denied_by to use `audits.reorder(id: :desc).first&.user_id`. Return nil if the user_id is nil. Use `User.find_by(id: user_id)&.name` for the lookup.
- Query
Placeholder. Replace the status subquery with the real KEY_BIS_CLAIM_DENIED value to list denied items that have no audits. Check the table and column names against db/schema.rb first.
SELECT mbi.id, mbi.status, mbi.updated_at FROM member_billing_items mbi LEFT JOIN audits a ON a.auditable_type = 'MemberBillingItem' AND a.auditable_id = mbi.id WHERE mbi.status = (SELECT id FROM reference_data WHERE false) GROUP BY mbi.id HAVING COUNT(a.id) = 0;
Queries are shown, never run. Check them before using them on production.
Ask a follow-up
Continues the same session, so it still remembers the error and the code it read.
Analysed 1 h ago · claude-sonnet-5-5 · $0.02