Skip to content

Dynamic Attributes

Carlos edited this page Aug 18, 2018 · 2 revisions

This method allows defining a conditional value that, if present in the result set, don't do anything. But, if it was not available, it will call its block in order to try returning the expected value. This features is great when used together with Auxiliary Statements.

How it works

Configurating

In any model, you just need to call dynamic_attribute method, passing an name for it and the block that will be called in case the attribute is not present.

# models/user.rb
class User < ActiveRecord::Base
  dynamic_attribute(:last_comment) do
    comments.order(id: :desc).first.content
  end
end

If the block is ever called, the value is stored for that record and won't be loaded again unless the record is wiped from ActiveRecord memory.

Using

To use the value is as normal as accessing any other attribute from a record.

User.first.last_comment                         # This will trigger 2 queries, one for the user and another for the attribute
User.with(:last_comment).first.last_comment     # This will trigger a single query
Clone this wiki locally