[Ruby on Rails] 5 idioms I always forget

Posted on August 24, 2017 , Tags: Ruby on Rails

Ruby on Rails is the great web application framework. I usually use it in the current job. But I always forget some idioms. In this post, I try to write out them.

1. Calling helper methods in Model methods

Idiom

ApplicationController.helpers.(helper name)

Example

2. Calling routing helper methods in Model methods

Idiom

Rails.application.routes.url_helpers.(routing helper name)

Example

3. Updating Model’s attributes without saving it to Database

Idiom

model.assign_attributes(attributes)

Example

4. Turning off updating updated_at when saving Models

Idiom

ActiveRecord::Base.record_timestamps = false

Example

5. Basic Auth

Idiom

authenticate_or_request_with_http_basic(realm = "Application", &login_procedure)

Example

And More…

Maybe there are more idioms I always googling them. If anyone who have a good idea to remember them, please tell me :)