... | @@ -7,8 +7,11 @@ This method allows defining a conditional value that, if present in the result s |
... | @@ -7,8 +7,11 @@ This method allows defining a conditional value that, if present in the result s |
|
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.
|
|
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
|
|
```ruby
|
|
dynamic_attribute(:last_comment) do
|
|
# models/user.rb
|
|
comments.order(id: :desc).first.content
|
|
class User < ActiveRecord::Base
|
|
|
|
dynamic_attribute(:last_comment) do
|
|
|
|
comments.order(id: :desc).first.content
|
|
|
|
end
|
|
end
|
|
end
|
|
```
|
|
```
|
|
|
|
|
... | | ... | |