AnApprentice
AnApprentice

Reputation: 110960

Any easy way to change a Name for a Model?

I've been building an app recently that has a model Books that is a database table and linked to several models/controllers. I recently learned that instead of Books it needs to be called publications (just an example)...

I now want to update the database and all the mentions throughout the app. Is there a simple way to do that in Rails 3. Or do I have to migrate that particular table (via version?) and manually update all the references throughout the app?

Thanks

Upvotes: 2

Views: 453

Answers (2)

vonconrad
vonconrad

Reputation: 25377

No, there's no way to automatically rename a model.

My suggestion would be to simply delete the model and recreate it with a new name, as that's usually easier than to rename all files generated when you create one. Make sure your migrations are correct, though!

As for the code, however, you'll have to do that the hard way. Some text editors can do search/replace over multiple files, which could be handy.

Upvotes: 0

Shreyas
Shreyas

Reputation: 8757

EDIT

You can continue using the same models and hence keep the references throughout the app. Just that, because of your new database schema you'll have to set the table name for the particular model.Also you can use the alias_attribute method, to so you can continue referring to the old attribute names even if you have changed the column names in your table.For ex:

class Book < ActiveRecord::Base
    set_table_name 'publications'
    set_primary_key 'id'
    alias_attribute :id,:publication_id
end

Upvotes: 1

Related Questions