RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

Rails plugin for reporting

ActiveRecord Statistics is my first plugin and it allows to easily define and pull statistics for ActiveRecord models. This plugin was built with reporting in mind.

Comments

Not to be a debbie downer, but what is the difference between this and named_scopes? Why not just call a named_scope and then add ‘.count’ to the end, or .count(:age)? I think this is a cool idea, but that’s the first thing that popped in my head.

Good question. So in the README I go into detail about the benefits of using the plugin (as opposed to named_scope). One of the main ones is that there is a standardized way to pull and filter statistics. So if you have an Account model and you want to report on average age, count of accounts, count of accounts subscribed to news letter, and like 10 other numbers. Using named_scope you would need a method call for each of those statistics (13 calls), this lets do it in one method call ( Account.statistics ). And if you need to only get all those statistics for users just in New York (or maybe another state) for accounts created after a certain date, you would need to create additional named scopes or pass those values into the conditions for each count call. Instead you could just do Account.statistics( :state => ‘NY’, :created_after => ‘2009-01-01’). That being said, this isn’t meant to replace named_scope and count altogether, it is focused reporting. In fact, it can be used in conjunction with named_scope so you don’t have to rewrite code code.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in