Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • T torque-postgresql
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 6
    • Issues 6
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Carlos
  • torque-postgresql
  • Wiki
  • Dynamic Attributes

Dynamic Attributes · Changes

Page history
Created Dynamic Attributes (markdown) authored Aug 18, 2018 by Carlos's avatar Carlos
Hide whitespace changes
Inline Side-by-side
Dynamic-Attributes.md 0 → 100644
View page @ 2b81538b
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
Clone repository
  • Wiki
  • Configuring

Data types

  • Enum
  • Interval

Querying

  • Distinct On
  • Auxiliary Statements