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

Gary Insley
Gary Insley
14,133 Points

ACTIVE RECORD - (Mysql2::Error: Table 'biller_development.employees_projects' doesn't exist) - Please help!!!!! :)

I am taking the course Active Record and following along with Hampton. We have got to the HAS_AND_BELONGS_T0_MANY stage. However im having a problem trying to access: Project.first.employee:

This is the code that i entered into IRB and the error message i received: (p = Project.first)

irb(main):003:0> p.employees
Mysql2::Error: Table 'biller_development.employees_projects' doesn't exist: SHOW FULL FIELDS FROM `employees_projects`
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'biller_development.employees_projects' doesn't exist: SHOW FULL FIELDS FROM `employees_projects`
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `query'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `block in execute'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `execute'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/mysql2_adapter.rb:228:in `execute'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:310:in `execute_and_free'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:451:in `columns'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/schema_cache.rb:93:in `block in prepare_default_proc'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/schema_cache.rb:44:in `yield'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/schema_cache.rb:44:in `columns'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/schema_cache.rb:97:in `block in prepare_default_proc'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/schema_cache.rb:50:in `yield'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/schema_cache.rb:50:in `columns_hash'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/associations/association_scope.rb:47:in `column_for'
... 7 levels...
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/associations/association.rb:97:in `association_scope'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/associations/association.rb:86:in `scope'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/associations/collection_association.rb:404:in `scope'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/associations/collection_proxy.rb:36:in `initialize'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/relation/delegation.rb:106:in `new'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/relation/delegation.rb:106:in `create'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/associations/collection_association.rb:36:in `reader'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/associations/builder/association.rb:110:in `employees'
    from (irb):3
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/Gary/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>' 

My EmployeesProjects Migration is set up as:

class EmployeesProjects < ActiveRecord::Migration
  def change
    create_table :employees_projects do |t|
      t.belongs_to :employee, :project
      t.timestamps
    end
  end
end

My Projects Module is set up as:

class Project < ActiveRecord::Base
  belongs_to :customer
  has_and_belongs_to_many :employees, :join_table => "employees_projects"
end

I believe i have followed along word for word and cannot seem to find where i have gone wrong, anyones help would be greatly appreciated.... I have been stuck on this for days and cannot progress.

Thanks

4 Answers

Not sure if you've already tried this, but if you ran your migration (using rake db:migrate from the command line) after you had already loaded Projects.first into your p variable, you may need to reload irb and reassign the variable to reflect any changes you've made.

Gary Insley
Gary Insley
14,133 Points

Hi Aimee,

Thanks for responding, ive been stuck on this for ages now. I've tried db:migrate again and now it is returning an error of:

NoMethodError: undefined method 'employee' for #<Project:.........

Any ideas?? Thanks

Gary Insley
Gary Insley
14,133 Points

I have added my files to the following GitHub Repository:

Your help would be greatly appreciated, thanks :)

https://github.com/garyinsley/treehouse-biller

Thanks for sharing your code, Gary!

So what's happening there is that you have a has_and_belongs_to_many relationship with Employee on your Project model. Since it can belong to many employees, not just limited to one, you need to access it through employees like:

Project.first.employees

which will return you a collection of all employees it belongs to :)

Gary Insley
Gary Insley
14,133 Points

Hi Aimee this is what i was doing, However i was first storing Project.first in a variable called p and then accessing employees on p:

p = Project.first
p.employees

However it was returning Mysql2::Error: Table 'biller_development.employees_projects' doesn't exist

If there is nothing wrong with my code is there anything else that could case this error??