Scope conditions
The scope conditions field is a ruby fragment that allows you to fine-tune the query that will be used for building your graph, query or pie.
Filter using conditions on columns
The primary use of the scope conditions is filtering the records.
Let's say we selected the users table which corresponds to the following model :
User(id: integer, confirmed_at: datetime, pseudo: string, email: string, age: integer, created_at: datetime, updated_at: datetime)
You can build conditions by combining a column name and a condition suffix like so :
pseudo_equals("Robix") pseudo_does_not_equal("Robix") pseudo_like("Rob") age_greater_than(37) age_less_than_or_equal(37) confirmed_at_not_nil
You can then chain these conditions :
pseudo_begins_with('R').created_at_after("2011-09-20").confirmed_at_not_nil
For a complete list of the condition suffixes and their aliases, you can refer to the convenient_scopes documentation.
Filter using conditions on columns from associations
DbInsights leans on Rails conventions to try and discover the associations between your models. You can then define conditions on the columns of an associated model.
Let's say we selected the payments table which corresponds to the following model :
Payment(id: integer, user_id: integer, amount: decimal, status: integer, created_at: datetime, updated_at: datetime)
You can graph the payments of the 37 year old users by specifying the following scope conditions :
user_age_is(37)
You are not limited to one association hop. If my users have a group_id column and thus belong to groups, I can graph the payments of the Old Timers group like so :
user_group_name_is('Old Timers')
Tuning the query with regular ActiveRecord methods
You can also tune your query with regular ActiveRecord 3 methods (see list here).
This is especially useful when building a query which is more free form than a graph or a pie. You can for instance select the columns that you want to see in the resulting table and limit the results :
select("pseudo, email").limit(20)