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 Single Table Inheritance

'drop table' not migrating when I run 'rake db:migrate' (6:30)

My migration file looks identical to Hampton's and yet when I run 'rake db:migrate' the 'drop_table(:customers)' and 'drop_table(:employees)' aren't migrating. Any idea why?

Also, as you'll see below, when I run 'Account.count' from the Rails console I get '0' whereas Hampton gets '2' (???).

Here's my code from Sublime:

class RemoveOldTables < ActiveRecord::Migration
  def up
    Customer.all.each do |c|
      Account.create(name: c.name, about: c.about)
    end

    Employee.all.each do |e|
      Account.create(name: e.name, email: e.email)
    end

    drop_table :customers
    drop_table :employees
  end

  def down
  end
end

And here's my output in Terminal:

biller [master] :> rake db:migrate
== 20151130193508 RemoveOldTables: migrating ==================================
== 20151130193508 RemoveOldTables: migrated (0.0000s) =========================

biller [master] :> rails console
Loading development environment (Rails 4.2.2)
cannot load such file -- awesome_print
2.2.1 :001 > Account.count
   (0.2ms)  SELECT COUNT(*) FROM `accounts`
 => 0 

1 Answer

Nelly Nelly
Nelly Nelly
7,134 Points

I had the same problem... and fount a solution :)

Maybe it could help people who would have the same issue: in rails c

ActiveRecord::Migration.drop_table(:employees)
ActiveRecord::Migration.drop_table(:customers)

And then create by hand new Accounts

Accounts.create( name: "John", email: "john@doe.com")

And then you can update like Hampton explains:

 a = Account.first  

  Account Load (0.4ms)  SELECT  `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 1 LIMIT 1
=> #<Account id: 1, type: nil, name: "Mark", email: "mark@mark.com", about: nil, created_at: "2016-07-29 21:54:26", updated_at: "2016-07-29 21:54:26">

 a.type = "Employee"    
=> "Employee"

a.save     
   (0.2ms)  BEGIN
  SQL (0.4ms)  UPDATE `accounts` SET `type` = 'Employee', `updated_at` = '2016-07-29 21:56:14' WHERE `accounts`.`id` = 1
   (2.0ms)  COMMIT
=> true