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)?
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.
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.
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.
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"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With