35

This is really a question about naming conventions.

I have a model called PromotedEvents

The file is called promoted_events.rb

I created the table with:

create_table :promoted_events do |t|

Now I'm having problems creating anything, so I'm wondering if theres some problem using model with two words

im in the console and tried

a = PromotedEvents.new

a = Promoted_Event.new

a = promoted_event.new

and keep getting a nameerror : uninitialized constant error

Any ideas?

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
ChrisWesAllen
  • 4,935
  • 18
  • 58
  • 83

4 Answers4

54

Your class should be singlular.

Name it PromotedEvent in the file promoted_event.rb

a = PromotedEvent.new
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • What is the terminal command to create a 2 word model? is it `rails generate model Model_Name` or some other combination? Are both words supposed to be capitalized? Do I put the underscore when generating the model? – tbaums Feb 21 '11 at 07:10
  • 2
    I think you can use either `ModelName` or `model_name`, and the right magic will happen. But `Model_Name` is not right (camel case or underscores, not ever both). Think of it like creating the model class, so it would be singular since it has to match the class name. – Alex Wayne Feb 21 '11 at 08:25
  • 2
    Yes, @AlexWayne is right. Both `CamelCase` or `under_score` usage is correct according to the [model generator description](https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/model/USAGE). – vlz Nov 23 '13 at 12:48
22

Model names are singular and camel case like so pe = PromotedEvent.new()

the file should be promoted_event.rb

Controllers are plural

PromotedEventsController

constants are ALL_CAPS

locals are separated_by_underscores_and_lowercase

table names are plural 'SELECT * FROM promoted_events`

loosecannon
  • 7,683
  • 3
  • 32
  • 43
  • 1
    what about attributes within the model. i.e. rails g model PromotedEvent name_of_event:string --------------> or should it be NameOfEvent:string ? any ideas of what is the syntax is here ? – BenKoshy Jun 22 '16 at 03:43
  • it should be just name:string, but if you need two or more words they are lowercase and separated by underscores, i.e. promotional_code:integer – Rcordoval Feb 07 '17 at 16:34
10

If it helps, I always think of it like this:

The model name is singular because it represents a single, specific thing. So, PromotedEvent is a specific promoted event that has a name, date, etc.

The table name on the other hand is plural. This is because the table stores a collection of these singular items. So, promoted_events.

In rails, filenames are mostly a matter of convention since ruby has pretty lax rules in this regard, but generally it's class_name.rb. This page might help you get a better overview of what conventions are used where and what is specific to Ruby versus Rails.

markquezada
  • 8,444
  • 6
  • 45
  • 52
1

If you are an extreme rails n00b like me, then you will want to remember to create a class definition for your newly created table and place it in app/models.

It would go like

class LargeCat < ActiveRecord::Base
    belongs_to :zoo
end
Danny
  • 3,982
  • 1
  • 34
  • 42