Search code examples
ruby-on-railsruby-on-rails-3database-designactiverecordactivemodel

Fat model, activeModel or in the controller?


My question has to do with data modeling.

We have a Score-model which links to both a Division model and a Element model. Between the Division and the Element model a habtm-relation exists. The score has to be evaluated against an attribute of the Element model (criterium attribute).

Now we want to introduce the concept of "Problems". A problem is each score which does not pass the criterium. (additional info: Multiple scores will be recorded over time, different types of scores exist. Filters on score_types and date ranges are desired)

What is the best way to model the Problem concept?

  1. Just calculate the problems in the controller each time the view of the problems is requested
  2. Introduce a Problems-model (which does not really contain any new information from the Score and Element info
  3. Create a tableless Model like ActiveModel.
  4. None of the above, as it is a newbie question and you should do it like so and so ... (fill in the blanks)

Any help would be much appreciated.


Solution

  • Without knowing what your codebase looks like and assuming the Problem model would only be storing the id of an associated Score, I'm going to suggest using a scope on your Score model. You can create the ARel query (or queries) to get the desired Scores without adding too much more code. Check the Rails Guide on Active Record Querying - Scopes section for more info.

    If you need the Problem model to do anything that the Score can't do, I'd consider creating an actual model (persisted or not is another decision).