Home > front end >  Rails naming convention misconception
Rails naming convention misconception

Time:07-26

i'm new to rails and web dev in general and since i've started learning i had a lingering question.. in tutorials when they generate a model (User for example) from the cmd with the command --> "rails generate model User etc...." the class name Starts with capital and the file name is in small letters (user.rb) and when creating relations they use the small letters one. i've looked online but it must've been a dumb question or is there something that i'm missing. Thanks!

CodePudding user response:

This is not just a Rails convention, but a Ruby one.

  • Class and module names are CamelCase, for example MyAwesomeClass (reference).
  • Filenames are snake_case.rb, for example my_awesome_class.rb (reference).
  • variable names are lowercase snake_case (reference).
  • method names are lowercase snake_case (reference).
  • Constants (other than modules or classes) are SCREAMING_SNAKE_CASE (reference).

CodePudding user response:

Just as note to naming convention

Rails use convention over configuration, but sometimes you need to customize names

For example

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym "RESTful"
end

If don't use such config, you need to use r_e_s_tful.rb with RESTful class

But using it, you can define RESTful in restful.rb without problems

  • Related