Render partial from cache substantially faster.
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, cached: true %>
When cached: true
present, Rails will use read_multi
to the cache store instead of reading from it every partial.
_/app/views/todos/todo.html.erb
<% cache todo do %>
<%= todo.name %>
<% end %>
Related cards:
Essentials Of Cache Expiration In Rails
Broadly, there are only three approaches to cache expiration. Each has overlapping use cases and varying granularity:
- Key Based - Everything that is cached is referenced by a unique name, this is the key. The key is composed of parts that ...
Presenter from Railscasts
Let's say we have a user page with different textual and graphical information. Usually it means there will be a lot of Ruby code with conditional logic inside .erb template. Sure it is not a good thing and will create a significant overhead durin...
Form Object from Railscasts
Models in a Rails application can easily become very complicated as more logic gets added to them. Fortunately there are several ways to refactor them that can help to clean up them up. Most of the behaviour in the User
model of our example appl...
Warning when fetching big result set from a database.
With large data set we can run into memory issue. Here is an example.
>> Post.published.count
=> 25000
>> Post.where(published: true).each do |post|
post.archive!
end
# Loads 25000 posts in memory
**Rails 5 adds warning whe...
Reusable Object-Oriented Systems
Simple Inheritance
require 'json'
module MovieFacts
class Director
def initialize(json)
@raw_data = JSON.parse(json)
end
def name
@raw_data.fetch('name')
end
def id
@raw_data.fetch('id')
end
...
Support OR in Active Record
Rails 5 has added OR method to Active Relation for generating queries with OR clause.
>> Post.where(id: 1).or(Post.where(title: 'Learn Rails'))
SELECT "posts".* FROM "posts" WHERE ("posts"."id" = ? OR "posts"."title" = ?) [["id", 1], ["ti...
Bi-directional destroy dependency
In Rails 4.x, it is not possible to have destroy dependency on both sides of a bi-directional association between the two models as it would result in an infinite callback loop causing SystemStackError: stack level too deep
.
class User < Ac...
ArrayInquirer provides friendlier way to check contents in an array
Rails 5 introduces ArrayInquirer
that wraps an array object and provides friendlier methods to check for the presence of elements that can be either a string or a symbol.
pets = ActiveSupport::ArrayInquirer.new([:cat, :dog, 'rabbit'])
> pe...
Decorating ActiveRecord
It’s common to use the decorator/presenter pattern to wrap ActiveRecord
objects and add view-specific logic. For example:
class Profile < SimpleDelegator
def title
"#{first_name} #{last_name}'s profile"
end
end
# controller
class P...
Primitive obsession
What is primitive obsession?
Primitive Obsession is using primitive data types to represent domain ideas. For example, we use a String to represent a message, an Integer to represent an amount of money, or a Struct/Dictionary/Hash to represent ...