Dipet
Dipet

Reputation: 323

Naming/Capitalization usage in Ruby on Rails 4

much to quite a few people's disappointment. I've decided to learn Ruby on Rails (I've been told by many people it's pointless to learn it, but ruby seems easy to get into and rails is a fun framework).

So I'm just now slowly starting to wrap my head around the naming convention with singular being used for the model and plural being used for the controller.

One thing I'm having a bit of trouble finding concise information on is Capitalization vs lowercase usage.

Example being

@order = Order.create(order_date: Time.now, customer_id: @customer.id)

Why exactly is 'Order.create' capitalized but not 'order_date'?

When to invoke capitalization has really got me confused when I use the rails console. I don't know when I'm suppose to be capitalizing or why. I want to say it too has to do with the model/controller naming scheme but neither of them are plural so that can't be the answer right?

Thanks for any help that is given, I really would/do appreciate it.

Upvotes: 0

Views: 2262

Answers (3)

Ismael
Ismael

Reputation: 16720

This is more a ruby thing actually. In ruby you use capitalization (camel case) for class and module names. And snake_case for method and variable names.

In your specific case Order is a class, create is a method, @order is an instance variable. order_date and customer_id are symbols that represent the columns on the database.

Upvotes: 0

Harsh Gupta
Harsh Gupta

Reputation: 4538

This is to do with naming conventions in Ruby and/or Rails.

Typically:

  • Create classes, modules using CamelCase.
  • Create methods, variables using underscore_case.
  • Create constants using UPPER_UNDERSCORE_CASE.

Take a look at different methods available in ActiveSupport to convert a string into various cases. http://apidock.com/rails/v4.1.8/String/camelize

Read Naming and Schema Conventions at Rails Guide.

A more succinct information.

HTH

Upvotes: 2

August
August

Reputation: 12558

  • CamelCase is used for classes and modules (e.g. String, Array, etc.)
  • snake_case is used for variables and methods (symbols are also usually snake_case)
  • SCREAMING_SNAKE_CASE is used for naming constants (e.g. STDOUT)

Upvotes: 2

Related Questions