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 Migrations and Relationships Relationships

alborz
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
alborz
Full Stack JavaScript Techdegree Graduate 30,885 Points

When I run Customer.first.time_entries in Rails console, I get an error

Hi,

I am able to successfully run Customer.first as you can see here:

irb(main):001:0> Customer.first Customer Load (0.6ms) SELECT `customers`.* FROM `customers` ORDER BY `customers`.`id` ASC LIMIT 1 => #<Customer id: 1, name: "Bob's Emporium", about: nil, balance: nil, created_at: "2014-12-15 05:55:51", updated_at: "2014-12-15 05:55:51", email: "bob@emporium.com">

However, when I try and run Customer.first.time_entries I get the following:

irb(main):002:0> Customer.first.time_entries Customer Load (0.4ms) SELECT `customers`.* FROM `customers` ORDER BY `customers`.`id` ASC LIMIT 1 TimeEntry Load (0.5ms) SELECT `time_entries`.* FROM `time_entries` WHERE `time_entries`.`customer_id` = 1 Mysql2::Error: Unknown column 'time_entries.customer_id' in 'where clause': SELECT `time_entries`.* FROM `time_entries` WHERE `time_entries`.`customer_id` = 1 ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'time_entries.customer_id' in 'where clause': SELECT `time_entries`.* FROM `time_entries` WHERE `time_entries`.`customer_id` = 1 from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `query' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `block in execute' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `execute' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/mysql2_adapter.rb:228:in `execute' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/mysql2_adapter.rb:232:in `exec_query' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/mysql2_adapter.rb:240:in `select' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/querying.rb:39:in `find_by_sql' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation.rb:611:in `exec_queries' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/association_relation.rb:19:in `exec_queries' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation.rb:493:in `load' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation.rb:238:in `to_a' ... 6 levels... from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in `start' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:69:in `console' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands.rb:17:in `<top (required)>' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require' from /Users/alborz/Desktop/Project_Files/biller/bin/rails:8:in `<top (required)>' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:241:in `load' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:241:in `block in load' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:241:in `load' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require' from /Users/alborz/.rbenv/versions/2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'

I'm not sure if I missed any step along the way.

Thanks!

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You should not manually change anything in schema.rb or in any existing migration. Instead, create a new migration file (```rails g migration FixColumnName) and inside put something like this:

  def change
    rename_column :time_entries, :customer_id_id, :customer_id
  end

Then run rake db:migrate and see if that worked. Remember that all time entries will have this set to nil after this operation (they will not have any customers assigned)

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You either didn't define proper relations in the models or one of the models or you did not write/migrate a proper migration.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

This from your error: Unknown column 'time_entries.customer_id' tells me you probably don't have customer_id column in the time_entries schema. You can check that in your db/schema.rb file. If that is true, just create a new migration which adds this column. If such migration exists but was not migrated, this command will tell you this: rake db:migrate:status

alborz
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
alborz
Full Stack JavaScript Techdegree Graduate 30,885 Points

Apparently this is what I have in the db/schemba.rb file for the time_entries schema:

 create_table "time_entries", force: true do |t|
    t.float    "time",           limit: 24
    t.integer  "customer_id_id"
    t.integer  "employee_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

For some reason "customer_id" displays as "customer_id_id".

Also, when I run rake db:migrate:status:

database: biller_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20141215053055  Create customers
   up     20141215054057  Add email address
   up     20141215054753  Create time entries
   up     20141215055244  Create employees

I learned that the schema is generated automatically, so manually changing that line of code wouldn't do anything if that's what's needed, would it?

Thanks