Support OR in Active Record

Posted Almost 8 years ago by Alexander M.

Rails 5 has added OR method to Active Relation for generating queries with OR clause. >> Post.where(id: 1).or(Post.where...

ArrayInquirer provides friendlier way to check contents in an array

Posted Almost 8 years ago by Alexander M.

Rails 5 introduces ArrayInquirer that wraps an array object and provides friendlier methods to check for the presence of elements...

Transactional fixtures renamed to transactional tests

Posted Almost 8 years ago by Alexander M.

In Rails 4.x we have transactional fixtures that wrap each test in a database transaction. This transaction rollbacks all...

A hidden field on collection radio buttons

Posted Almost 8 years ago by Alexander M.

Consider the following form which has only one input role_id which is accepted through collection_radio_button.

Bi-directional destroy dependency

Posted Almost 8 years ago by Alexander M.

In Rails 4.x, it is not possible to have destroy dependency on both sides of a bi-directional association...

Update a record without updating timestamps

Posted Almost 8 years ago by Alexander M.

In Rails 4.x, when we save an ActiveRecord object then Rails automatically updates fields updated_at or updated_on...

Warning when fetching big result set from a database.

Posted Almost 8 years ago by Alexander M.

With large data set we can run into memory issue. Here is an example. >> Post.published.count => 25000 >> Post.where(published: true).each...

Render partial from cache substantially faster.

Posted Almost 8 years ago by Alexander M.

For example we have a view and a partial view named index.html.erb and _todo.html.erb. /app/views/todos/index.html.erb <%= render partial: 'todo', collection: @todos...

Generate unique random token.

Posted Almost 8 years ago by Alexander M.

We sometimes need unique and random tokens in our web apps. Here is how we typically build it.

Improve `redirect_to :back` with new `redirect_back` method.

Posted Almost 8 years ago by Alexander M.

### Rails 4.x ### class PostsController < ApplicationController # HTTP_REFERER is not present and it throws exception rescue_from ActionController::RedirectBackError, with...

Models `belongs_to` association required by default.

Posted Almost 8 years ago by Alexander M.

In Ruby on Rails 5 belongs_to associations required by default so the following code: class User < ApplicationRecord end