Rails unorthodox naming of models with abbreviations

扶醉桌前 提交于 2019-12-08 14:58:23

问题


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?


回答1:


Convention over configuration man. Suck it up.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1652347/rails-unorthodox-naming-of-models-with-abbreviations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!