Bad model name


#1

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…


#2
 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


#3

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


#4

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


#5

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