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 trialChris Vukin
17,787 Pointstotal_time not working
I'm having some difficulty with the total_time when following along with the vid at 26:46. The time is not being added in my build, any thoughts?
2.2.0 :021 > Account.all
Account Load (0.5ms) SELECT `accounts`.* FROM `accounts`
=> #<ActiveRecord::Relation [#<Customer id: 1, type: "Customer", name: "Bob's Emporium", email: nil, about: nil, created_at: "2015-02-01 02:58:51", updated_at: "2015-02-01 03:03:51">, #<Employee id: 2, type: "Employee", name: "Hampton", email: "hcatlin@gmailcom", about: nil, created_at: "2015-02-01 02:58:51", updated_at: "2015-02-01 03:05:06">]>
2.2.0 :022 > e.log_time(3, Customer.first)
Customer Load (0.6ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`type` IN ('Customer') ORDER BY `accounts`.`id` ASC LIMIT 1
=> #<TimeEntry id: nil, time: 3.0, customer_id: nil, employee_id: 2, created_at: nil, updated_at: nil, account_id: 1>
2.2.0 :023 > e.total_hours
(0.5ms) SELECT SUM(`account_entries`.`time`) FROM `account_entries` WHERE `account_entries`.`employee_id` = 2
=> 1.2000000476837158
2 Answers
Conrad Spotts
11,768 PointsIn what you're showing there, I'm not seeing any commit message after you run e.log_time(3, Customer.first)
When I run it, I see the following:
2.2.0 :020 > e.log_time(3, Customer.first)
Customer Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`type` IN ('Customer') ORDER BY `accounts`.`id` ASC LIMIT 1
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO `account_entries` (`time`, `employee_id`, `account_id`, `created_at`, `updated_at`) VALUES (3.0, 1, 2, '2015-02-04 21:12:05.075304', '2015-02-04 21:12:05.075304')
(0.3ms) COMMIT
=> #<TimeEntry id: 2, time: 3.0, customer_id: nil, employee_id: 1, created_at: "2015-02-04 21:12:05", updated_at: "2015-02-04 21:12:05", account_id: 2>
I was having trouble with the total_hours as well, but it turned out that my problem was the log_time. I had TimeEntry.new
instead of TimeEntry.create
which was causing log_time to not run. properly.
Double check employee.rb
class Employee < Account
has_many :time_entries
def total_hours
time_entries.sum(:time)
end
def log_time(amount, customer)
TimeEntry.create(time: amount, employee: self, account: customer)
end
end
Chris Vukin
17,787 PointsExcellent!! Thanks for the reply