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?
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.
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.
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
.
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