Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby ActiveRecord Basics AR Extensions Stateful Models

undefined method `update_balance!' for nil:NilClass

I typed the command - TimeEntry.all.each { |t| t.update_attributes(state: "submitted") }

and got the error-

TimeEntry Load (0.9ms) SELECT account_entries.* FROM account_entries WHERE account_entries.type IN ('TimeEntry') (0.3ms) BEGIN

Account Load (0.6ms) SELECT accounts.* FROM accounts WHERE accounts.id = 1 LIMIT 1

SQL (2.1ms) UPDATE account_entries SET state = 'submitted', updated_at = '2016-03-08 23:46:15' WHERE account_entries.id = 1 (2.6ms) ROLLBACK

NoMethodError: undefined method `update_balance!' for nil:NilClass

1 Answer

Damian Kosowski
Damian Kosowski
19,746 Points

Is it possible that you have TimeEntry in your database which doesn't have a specified account_id? The update_balance! method is called after saving the entry but it is called on the instance of the Account model. So if there is a TimeEntry without a specified account_id Rails is looking for the account of this time entry which is not present, hence nil.