# Model Coordination, A design problem

**URL:** <https://www.ruby-forum.com/t/model-coordination-a-design-problem/123293>\
**Category:** Rails\
**Created:** [December 16, 2007, 1:31am UTC](https://www.ruby-forum.com/t/model-coordination-a-design-problem/123293 "2007-12-16T01:31:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 16, 2007, 1:31am UTC](https://www.ruby-forum.com/t/model-coordination-a-design-problem/123293/1 "2007-12-16T01:31:10Z")

</div>

Greating All,

I have had to solve the following problem enough in the last month that  
I’m sure it has a name and a body of research behind it but I’m having  
trouble finding it.

The general problem is this:  
I have a Site Model. Each Site has many Consumers and Producers.  
Producers make some amount of a “resource” available at the Site, while  
Consumers use these “resources.” The program’s goal is to manage  
changes in Consumers and Producers to keep them in balance.

Questions:

1. When is it better to try to maintain a “resources” column in Site,  
and when is it better to reproduce the data every time. In other words  
is there a good way of “cashing” the resource calculation?

2. When a controller makes a change to a Consumer or Producer should the  
model or the controller be responsible for asking the changed items Site  
to rebalance its resources?

3. Is there a common name for this kind of problem that I can  
google/wikipedia for more information?

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 16, 2007, 8:26am UTC](https://www.ruby-forum.com/t/model-coordination-a-design-problem/123293/2 "2007-12-16T08:26:48Z")

</div>

On Dec 15, 6:31 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> changes in Consumers and Producers to keep them in balance.
> 
> Questions:
> 
> 1. When is it better to try to maintain a “resources” column in Site,  
> and when is it better to reproduce the data every time. In other words  
> is there a good way of “cashing” the resource calculation?

As a general rule, do not store redundant information in a database,  
or anywhere else, until you have a known performance problem.

> 1. When a controller makes a change to a Consumer or Producer should the  
> model or the controller be responsible for asking the changed items Site  
> to rebalance its resources?

This one is easy. The model should be responsible. Keep the  
controllers as thin as possible. Ideally a controller action should  
fetch a model, call one method on the model, and then call a view to  
render that one model.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 17, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/model-coordination-a-design-problem/123293/3 "2007-12-17T20:39:27Z")

</div>

kevin cline wrote:

> On Dec 15, 6:31 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > changes in Consumers and Producers to keep them in balance.
> > 
> > Questions:
> > 
> > 1. When is it better to try to maintain a “resources” column in Site,  
> > and when is it better to reproduce the data every time. In other words  
> > is there a good way of “cashing” the resource calculation?
> 
> As a general rule, do not store redundant information in a database,  
> or anywhere else, until you have a known performance problem.

A little testing over the weekend reveled that i have at least two cases  
where changing a single Producers output ended up hitting the database  
302 times and performing a sum across all producers or consumers 1437  
times taking 8.34s on my SQLite :memory: database. 82% of the run time  
was spent in one of the adding up resources either used or consumed, 11%  
in data access.

Given the above, I think the question becomes where and how to cache  
data. The concept of not storing redundant data seems prudent, but it  
looks like a need to do some kind of memory/processing time trade off,  
even it is not stored in the database.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 17, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/model-coordination-a-design-problem/123293/4 "2007-12-17T22:28:01Z")

</div>

What is involved in rebalancing resources? Maybe the algorithm could  
be improved.

///ark

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 17, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/model-coordination-a-design-problem/123293/5 "2007-12-17T23:04:55Z")

</div>

Check out the counter cache in Rails: [#23 Counter Cache Column - RailsCasts](http://railscasts.com/episodes/23)

On Dec 17, 2007 11:39 AM, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
