# Bad model name

**URL:** <https://www.ruby-forum.com/t/bad-model-name/162784>\
**Category:** Rails\
**Created:** [March 13, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/bad-model-name/162784 "2009-03-13T18:14:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tned](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@tned](https://www.ruby-forum.com/u/tned)\
**Post date:** [March 13, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/bad-model-name/162784/1 "2009-03-13T18:14:09Z")

</div>

I created model named “whats\_new”.

$ script/generate model whats\_new  
exists app/models/  
exists test/unit/  
exists test/fixtures/  
create app/models/whats\_new.rb  
create test/unit/whats\_new\_test.rb  
create test/fixtures/whats\_news.yml  
exists db/migrate  
create db/migrate/\*\*\*\*\*\_create\_whats\_news.rb

* * *

rake db:migrate  
WhatsNew.find()  
WhatsNew.new

There are no problem.

## However, test/unit/whats\_new\_test.rb

## whats\_news(:data1)

=\> “No class attached to find.”

* * *

## class Fixtures … … def initialize … @class\_name = class\_name || (ActiveRecord::Base.pluralize\_table\_names ? @table\_name.singularize.camelize : @table\_name.camelize) p @class\_name … end end

=\> “WhatsNews”  
A true class name is “WhatsNew”.

@table\_name == “whats\_news”

> > “whats\_news”.singularize.camelize  
> > =\> “WhatsNews”
> 
> > “whats\_new”.pluralize.singularize  
> > =\> “whats\_news”

By the way, I cannot speak English…

---

<div class="post-metadata">

**Author:** ![tned](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@tned](https://www.ruby-forum.com/u/tned)\
**Post date:** [March 13, 2009, 6:38pm UTC](https://www.ruby-forum.com/t/bad-model-name/162784/2 "2009-03-13T18:38:27Z")

</div>

> ```
> create db/migrate/ ***** _create_whats_news.rb
> 
> ```
> 
> test/unit/whats\_new\_test.rb  
> …
> 
> @table\_name == “whats\_news”
> 
> > > “whats\_news”.singularize.camelize  
> > > =\> “WhatsNews”
> > 
> > > “whats\_new”.pluralize.singularize  
> > > =\> “whats\_news”
> 
> By the way, I cannot speak English…

In english, ‘news’ is both singular and plural. A single news item is  
still called ‘news’ not ‘new’.

Two choices for you…

- Add your own inflection so that ‘news’ is singularlized to ‘news’.
- Change your class name.

-philip

---

<div class="post-metadata">

**Author:** ![tned](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@tned](https://www.ruby-forum.com/u/tned)\
**Post date:** [March 14, 2009, 12:35am UTC](https://www.ruby-forum.com/t/bad-model-name/162784/3 "2009-03-14T00:35:32Z")

</div>

Philip H. wrote:

> Two choices for you…
> 
> - Add your own inflection so that ‘news’ is singularlized to ‘news’.
> - Change your class name.

Keep in mind that model names should be nouns. Controller actions are  
verbs. In your case of WhatsNew is practically a complete sentence.  
“What is new?” Maybe use something like NewsItem or simply News, which  
rails will properly pluralize as “News.” In this case the table name  
will be news.

Then to find out, “What is new?” you can create a named\_scope that gets  
recent new items like this:

@recent\_news\_items = NewsItem.recent

or

@recent\_news\_items = News.recent

---

<div class="post-metadata">

**Author:** ![tned](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@tned](https://www.ruby-forum.com/u/tned)\
**Post date:** [March 14, 2009, 12:41am UTC](https://www.ruby-forum.com/t/bad-model-name/162784/4 "2009-03-14T00:41:26Z")

</div>

Robert W. wrote:

> Keep in mind that model names should be nouns. Controller actions are  
> verbs. In your case of WhatsNew is practically a complete sentence.

Correction: Controller actions aren’t necessarily verbs. It might be  
more accurate to say the handles what to do with the request and the  
verb is part of the request (i.e. “GET index” would get then index of  
news items and “POST create” would create new news items).

---

<div class="post-metadata">

**Author:** ![tned](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@tned](https://www.ruby-forum.com/u/tned)\
**Post date:** [March 14, 2009, 10:37am UTC](https://www.ruby-forum.com/t/bad-model-name/162784/5 "2009-03-14T10:37:31Z")

</div>

Thank you for everybody.

I change the class name.

Robert W. wrote:

> @recent\_news\_items = NewsItem.recent
> 
> or
> 
> @recent\_news\_items = News.recent  
> It did not hit on in me.  
> “What is new?” is in “News”.

P.S.  
I added  
rails/lib/rails\_generator/generators/components/model/  
model\_generator.rb

to

p ‘warning: class\_name’ unless class\_name ==  
class\_name.pluralize.singularize
