Forum: Ruby on Rails bad model name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
46cc6f9b83af6135f46868d30fb442c0?d=identicon&s=25 tned (Guest)
on 2009-03-13 18:14
(Received via mailing list)
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...
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-03-13 18:38
(Received via mailing list)
>      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
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-03-14 00:35
Philip Hallstrom 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
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-03-14 00:41
Robert Walker 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).
46cc6f9b83af6135f46868d30fb442c0?d=identicon&s=25 tned (Guest)
on 2009-03-14 10:37
(Received via mailing list)
Thank you for everybody.

I change the class name.

Robert Walker 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
This topic is locked and can not be replied to.