Showing posts with label query. Show all posts
Showing posts with label query. Show all posts

Advanced SQL and named scopes stack with ActiveRecord

If you ever work with rails application that is a little bit more complex then a simple CRUD you would know that some of the ActiveRecord magic doesn't work for complex SQL queries. I am primary talking about named scopes stack feature.

Double select from a single table in complex SQL query.

When I start learning SQL I have to write a lot of nested queries when working on complex select statements from a single table. Now, I feel more comfortable with it and show how to avoid nesting. There are cases when you have to compare one row of the table with all others to get the result:

  • Select next element to current by the value of some field

  • Select records that are unique by some complex expression

  • Select all records that have the same parameter as given record.


You can get rid of nesting in all above cases. Let's review the example.
Copyright © Spesial Unik. All rights reserved. Template by CB. Theme Framework: Responsive Design