Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby/Rails - Models Named with Two Words (Naming Convention Issues)

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?

like image 347
ChrisWesAllen Avatar asked Feb 04 '11 00:02

ChrisWesAllen


People also ask

What is the naming convention for models and tables in Rails?

Naming conventions in Active Record model Rails is capable of pluralizing (and singularizing) both regular and irregular words. Model class names must use the CamelCase form when composed of two or more words, while the database table names must use the snake_case form.

What's the proper convention for naming variables in Ruby?

Variable names in Ruby can be created from alphanumeric characters and the underscore _ character. A variable cannot begin with a number. This makes it easier for the interpreter to distinguish a literal number from a variable. Variable names cannot begin with a capital letter.

What do you mean by naming convention in Rails?

Controller class names use CamelCase and have Controller as a suffix. The Controller suffix is always singular. The name of the resource is usually plural. Controller actions use snake_case and usually match the standard route names Rails defines ( index , show , new , create , edit , update , delete ).

What are Ruby on Rails models?

A Rails Model is a Ruby class that can add database records (think of whole rows in an Excel table), find particular data you're looking for, update that data, or remove data.


1 Answers

Your class should be singlular.

Name it PromotedEvent in the file promoted_event.rb

a = PromotedEvent.new 
like image 174
Alex Wayne Avatar answered Sep 30 '22 09:09

Alex Wayne