views:

498

answers:

3

Hi,

I'm somewhat new to rails so please be gentle.

I'm working on an "analytics" page for a rails app. The analytics page does not persist any data of its own (it's very primitive at this point) but does utilize metrics that I'm grabbing from the DB (via the aggregate expressions built into ActiveRecord). Aside from gathering and presenting the metrics the only other requirement I have is to allow the user to provide a date range to filter the data. Up to this point I have been using instance variables and the like to store the metrics information...as the number of metrics grow along with the need to manage the start and end filter dates I begin thinking that I should put this data into its own model. If I do move all of my "data" into a model should I just use a plain object with attr_accessors or is there a more appropriate base class I could use for non-persistent data? I'm familiar enough with a MVC architecture to know that my controller is getting to bloated but no familiar enough with rails to determine how I should organize my data/logic in this case.

Any insight would be greatly appreciated!

+5  A: 

It sounds like you could use a Rails non active-record model. There's a good Railscast about that :

http://railscasts.com/episodes/121-non-active-record-model

Hope that helps,

Chris Lowis
+1  A: 

You're on the right track here. Many applications have classes inside app/models that do not inherit from ActiveRecord::Base. Any time you find yourself managing lots of arbitrary variables inside controller actions, it's a good place to consider abstracting that data into a non-persistent model.

Aaron Longwell
A: 

This is an area that's not well documented at present, probably because the ActiveRecord stuff is sexier?

I went through the same process, finding my controller actions were becoming uncomfortably large and full of logic as I strove to construct my derived data from ActiveRecord-based models, which in turn started to acquire additional responsibilities that they didn't really want or need.

Once I got the same advice you're getting now the whole thing simplified magnificently.

Mike Woodhouse