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 Callbacks Using Callbacks

My amount got truncated! What's going on?

I was following along with adding "calculate_amount!" into the program, but I received an odd result.

My first time entry was 1.2 hours, resulting in 1.2 * 34 = 40.8. But for some reason only 40 was saved.

>> t = TimeEntry.first
  TimeEntry Load (0.4ms)  SELECT  `account_entries`.* FROM `account_entries` WHERE `account_entries`.`type` IN ('TimeEntry')  ORDER BY `account_entries`.`id` ASC LIMIT 1
=> # TimeEntry id: 1, time: 1.2, customer_id: 1, employee_id: 2, created_at: "2015-09-22 05:38:50", updated_at: "2015-09-23 01:49:05", account_id: 1, type: "TimeEntry", amount: nil
>> t.calculate_amount!
=> 40.8
>> t
=> # TimeEntry id: 1, time: 1.2, customer_id: 1, employee_id: 2, created_at: "2015-09-22 05:38:50", updated_at: "2015-09-23 01:49:05", account_id: 1, type: "TimeEntry", amount: 40
>> t.save
   (0.2ms)  BEGIN
  Account Load (1.2ms)  SELECT  `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 1 LIMIT 1
  Account Exists (10.0ms)  SELECT  1 AS one FROM `accounts` WHERE (`accounts`.`name` = BINARY 'Bob\'s Emporium' AND `accounts`.`id` != 1) LIMIT 1
  SQL (5.6ms)  UPDATE `account_entries` SET `amount` = 40, `updated_at` = '2015-09-25 22:33:39' WHERE `account_entries`.`id` = 1
   (10.7ms)  COMMIT
=> true

For some reason this is not displaying properly. I had to remove the angle brackets. Anyway...

Only 40 was saved in the variable after the calculation returned 40.8, and then again only 40 was added to the database.

1 Answer

The only thing I can see if if you are saving the amount as an int or a float. Perhaps see if it is an int and change it to a float?

It is a decimal. I'm following the video. In the schema it is listed as t.decimal "amount", precision: 10.