Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby / Rails naming conventions

If I generate a scaffold called "product", what are the differences between the following when I use them in coding my app? @products, @product, @Product, @Products, Products, Product, product, and products (I'm pretty sure those aren't all used, but it should at least give the idea of what I'm referring to).

I can't seem to find a simple explanation of what each combination of @/capitalization/plurality means. Help would be greatly appreciated.

like image 451
detarallt Avatar asked Nov 15 '12 18:11

detarallt


3 Answers

You can read Ruby style guide and Rails style guide, and you get answer on your question.

like image 64
Alexander Randa Avatar answered Oct 04 '22 22:10

Alexander Randa


The only reserved word is capitalized Product and this references the object model. For example to get all products and save it in the variable @products you would do:

@products = Product.all
@product = Product.find(1)

If you left off the @ sign off of 'products' then it would be saved only as a local variable and your view would not be able to access it.

In this example @products and @product are variables you declare in the controller. You could just use:

@lotsofproducts = Product.all
like image 34
zarazan Avatar answered Oct 04 '22 22:10

zarazan


For Ruby naming conventions, you can check here: http://rubylearning.com/satishtalim/ruby_names.html

As for Rails' naming conventions, it's linguistic: Model - singular, Controller - plural, DatabaseTable - plural(snake_case), View(directory name) - plural. You can check from http://guides.rubyonrails.org/

like image 30
Jing Li Avatar answered Oct 04 '22 21:10

Jing Li