Bryan Ward
Bryan Ward

Reputation: 6701

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?

Upvotes: 20

Views: 3356

Answers (3)

Mike
Mike

Reputation: 9852

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.

Upvotes: 44

allesklar
allesklar

Reputation: 9600

Yes you should use XmlElement.

Not only for this example but about every aspect of an application it will never pay off to veer off conventions. There is so much 'magic' going under Rails' hood that it's just not worth it.

Upvotes: 2

user6212
user6212

Reputation:

Convention over configuration man. Suck it up.

Upvotes: 7

Related Questions