|
|
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](https://github.com/crashtech/torque-postgresql/wiki/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.
|
|
|
|
|
|
```ruby
|
|
|
dynamic_attribute(:last_comment) do
|
|
|
comments.order(id: :desc).first.content
|
|
|
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.
|
|
|
|
|
|
```ruby
|
|
|
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
|
|
|
``` |
|
|
\ No newline at end of file |