# Any easy way to change a Name for a Model?

**URL:** <https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263>\
**Category:** Rails\
**Created:** [September 20, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263 "2010-09-20T18:29:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 20, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263/1 "2010-09-20T18:29:55Z")

</div>

Hello, 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

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 20, 2010, 7:32pm UTC](https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263/2 "2010-09-20T19:32:21Z")

</div>

nobosh wrote:

> Hello, I’ve been building an app recently that has a model Books

That should be Book. Rails model names are singular by convention.  
Table names are plural.

> that  
> is a database table

No! Models are not database tables; please don’t think of them that  
way. They _refer_ to database tables, but the Ruby objects and the DB  
records and tables are not the same thing.

> 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?)

Version? Huh? Just write a migration that changes the name of the  
table.

Note also that you can use set\_table\_name to specify a different table  
name than the one Rails would expect. Thus, you can change the class  
name or the table name without also changing the other. I wouldn’t  
advise breaking the Rails conventions unless you have a good reason to.

> and manually update all  
> the references throughout the app?

Yup. Your automated tests will break when they encounter the old class  
name and let you know where to change it.

> Thanks

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 20, 2010, 10:47pm UTC](https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263/3 "2010-09-20T22:47:32Z")

</div>

Thanks Marnen. Regarding writing a migration that changes the name of  
the table, for some reason I feel like this situation is going to  
happen frequently and that could fill up the migrations. GIven that  
I’m only a few weeks into development and I have no real data, for  
some reason I feel it’d be cleaner to revert the book migration and  
add one that’s correct?

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 20, 2010, 11:06pm UTC](https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263/4 "2010-09-20T23:06:16Z")

</div>

migrations dont fill up , and if you want to get the result of all  
migrations on one go , do rake db:schema , it will load the schema.

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 20, 2010, 11:23pm UTC](https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263/5 "2010-09-20T23:23:43Z")

</div>

nobosh wrote:

> Thanks Marnen. Regarding writing a migration that changes the name of  
> the table, for some reason I feel like this situation is going to  
> happen frequently and that could fill up the migrations.

You can’t fill up migrations.

> GIven that  
> I’m only a few weeks into development and I have no real data, for  
> some reason

For what reason?

> I feel

Programming is not generally about feelings.

> it’d be cleaner to revert the book migration and  
> add one that’s correct?

You could in this case, yes. I normally don’t advise it. And of course  
it would be suicidally stupid to do so I. The general case where you do  
have data.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 21, 2010, 2:22pm UTC](https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263/6 "2010-09-21T14:22:40Z")

</div>

You also have to search and replace all occurrences of Books. I hope you  
have some tests to back you up.

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 20, 2010, 11:32pm UTC](https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263/7 "2010-09-20T23:32:02Z")

</div>

On Mon, Sep 20, 2010 at 4:23 PM, Marnen Laibow-Koser  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Programming is not generally about feelings.

I feel good when I use Ruby, I feel bad when I use PHP.

🙂

–  
Greg D.  
[destiney.com](http://destiney.com) | [gregdonald.com](http://gregdonald.com)

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 21, 2010, 2:23pm UTC](https://www.ruby-forum.com/t/any-easy-way-to-change-a-name-for-a-model/195263/8 "2010-09-21T14:23:44Z")

</div>

> > Programming is not generally about feelings.
> 
> I feel good when I use Ruby, I feel bad when I use PHP.

Stop sleeping with your computer!
