Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails unorthodox naming of models with abbreviations

In an application I am building I am storing an XML file in my database using the acts_as_tree association. I would like to name the class XMLElement but this throws rails off since the capitalization is non-standard. It is looking for XMLElement from the file name xml_element.rb. I tried changing the filename to x_m_l_element.rb to try and trick it into thinking that "XML" was really two words, but this didn't work either. Should I just suck it up and use the name XmlElement instead of the more ideal XMLElement, or is there a better way around this issue?

like image 777
Bryan Ward Avatar asked Oct 30 '09 21:10

Bryan Ward


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 are the naming conventions in Ruby?

General naming conventions in RubyClass names and module names use PascalCase. For example, ApplicationController. Method and variable names use snake_case. For example, attr_accessor.


1 Answers

Add the following to config/initializers/inflections.rb.

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'XML'
end

Now running $ rails g model XMLElement… will create a class named XMLElement in a file named xml_element.rb and an associated table xml_elements.

like image 161
Mike Avatar answered Oct 03 '22 05:10

Mike