Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails multi word Model naming conventions

I have a lookup table called metadata_types in my database, which lists all the various types of metadata my app uses.

Metadata is the plural of Metadatum; should I have called the table metadatum_types? In other words should the plurality be on both words in this case, or just the second? Should the corresponding model then also be metadatum_type.rb (Class MetadatumType)?

like image 499
cman77 Avatar asked Jun 25 '12 19:06

cman77


People also ask

How do you name a module in Rails?

Classes and modules use MixedCase and have no underscores, each word starts with a uppercase letter. Database Table - e.g. invoice_items, orders Table names have all lowercase letters and underscores between words, also all table names need to be plural.

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.

Should controller names be plural?

Controller names are either singular or plural : Using “rails g resource” names the controllers plural, which makes sense because I think about them controlling many routes. Resource names are singular : They create a lot of mvc framework, the name you pass will become the model name, and let rails pluralize the rest.


2 Answers

metadata_types for the table name is fine. From there, you can do

> "metadata_types".classify
=> "MetadataType"

so MetadataType would be the class name expected by rails.

This also works in reverse:

> "MetadataType".tableize
=> "metadata_types"
like image 90
x1a4 Avatar answered Sep 27 '22 20:09

x1a4


metadatum_types is fine. Rails only applies pluralization at the end of the model name.

The method is rather simple and can be found here: http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-pluralize (code)

The inflections Rails applies to the name can be found here: Inflector.rb

like image 37
Tigraine Avatar answered Sep 27 '22 21:09

Tigraine