4 ms·
I've started using a rails model backed by a SQL view anytime I need to do something more complicated than book.author.name with active record. Then I can trea
by learc83 8y ago
I've started using a rails model backed by a SQL view anytime I need to do something more complicated than book.author.name with active record.
Then I can treat it just like any other rails model. If you add a controller and a route you can expose it just like you would any other read restful resource with the caveat that it's read only.
As an added bonus for analytics that don't need to be real-time I can turn the view into a materialized view that updates periodically for easy caching.
I'm using the Scenic gem to handle view versioning, which all let's me write all of my complicated queries in a dedicated SQL file instead of an embedded string without syntax highlighting.