0

Using Rails 3, I've changed the name of a table in the model like this:

# app/models/product.rb
class Product < ActiveRecord::Base
  set_table_name "items"
end

But when I try setting up tests, I get the following error:

Started
E
Finished in 0.027396 seconds.

  1) Error:
test_the_truth(CustomerTest):
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'project2_test.products' doesn't exist: DELETE FROM `products`


1 tests, 0 assertions, 0 failures, 1 errors

Any idea how I can let it know about Products?

Brandan
  • 14,735
  • 3
  • 56
  • 71
fatfrog
  • 2,118
  • 1
  • 23
  • 46
  • possible duplicate of [How do you write a migration to rename a Model and its table in Rails?](http://stackoverflow.com/questions/471416/how-do-you-write-a-migration-to-rename-a-model-and-its-table-in-rails) – Schwern Feb 27 '12 at 02:52

2 Answers2

1

OK found the answer here:

http://www.missiondata.com/blog/systems-integration/80/rails-fixtures-with-models-using-set_table_name/

Had to change the name of the Fixture yml file from Products to Items.

fatfrog
  • 2,118
  • 1
  • 23
  • 46
0

Rather than alter the class directly, you should create a migration. This will allow Rails to smoothly change the database, and allow any others working on the project to change their database in the same manner.

Write a change method which uses rename_table.

class RenameProductsToItems < ActiveRecord::Migration
  def change
    rename_table :items, :products :string
  end
end
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Thanks - the reason it's like this is that we are re-building an App, and it has to run at the same time on the same db as the old one until we migrate over. – fatfrog Feb 27 '12 at 02:54