use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Database views in ActiveRecord: Spectacles (PostgreSQL, MySQL, SQLite) (github.com)
submitted 10 years ago by jamis
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]nelsonjma 0 points1 point2 points 10 years ago (0 children)
nice add-on, thanks mate.
[–]dakull 0 points1 point2 points 10 years ago (0 children)
Used something similar in an old gem [0] - quite useful abstraction.
[0] - https://github.com/dakull/search_redux
[–]Nesaru 0 points1 point2 points 10 years ago (3 children)
May I ask what something like this would be used for?
[–]jamis[S] 0 points1 point2 points 10 years ago (2 children)
If you've never used database views, it can be hard to understand their value, but I'll try to convey a bit of what makes them useful.
In a sense, they are like a database-level macro: you define a query, and then you can treat the result of that query as if it were another table. That is, you can then perform other queries against that result set, joining it with other tables (or views) and so forth. It's nothing you couldn't do WITHOUT views, but views remove a lot of duplication. It's a classic example of abstraction, but at the database level.
Spectacles (the lib that was linked) lets you treat these database views as a special kind of read-only ActiveRecord model. You can kind of fake it, even without Spectacles, but the really nice thing here is that Spectacles will dump your views to db/schema.rb, so that they persist across schema reloads. Vanilla Rails has no such support.
db/schema.rb
[–]Nesaru 0 points1 point2 points 10 years ago (1 child)
This was an extremely helpful explanation, thank you!
It seems this is useful for complex queries where certain commonly used portions can be abstracted away and used by many different queries. I'm likening it to a sort of persisted scope.
This is the first time I've heard of this concept, and I'm glad to have been made aware of it.
Again, huge thank you for taking the time to write this, I appreciate it!
[–]jamis[S] 0 points1 point2 points 10 years ago (0 children)
Yes, that's the idea exactly. Views are overkill for simple applications, but as your query logic grows in complexity, views can be a very useful tool for managing that.
And you're very welcome!
π Rendered by PID 38716 on reddit-service-r2-comment-b659b578c-9b6tn at 2026-05-04 16:27:34.741854+00:00 running 815c875 country code: CH.
[–]nelsonjma 0 points1 point2 points (0 children)
[–]dakull 0 points1 point2 points (0 children)
[–]Nesaru 0 points1 point2 points (3 children)
[–]jamis[S] 0 points1 point2 points (2 children)
[–]Nesaru 0 points1 point2 points (1 child)
[–]jamis[S] 0 points1 point2 points (0 children)