# Associations over 37 different tables with has\_one

**URL:** <https://www.ruby-forum.com/t/associations-over-37-different-tables-with-has-one/172199>\
**Category:** Rails\
**Created:** [July 16, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/associations-over-37-different-tables-with-has-one/172199 "2009-07-16T16:42:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 16, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/associations-over-37-different-tables-with-has-one/172199/1 "2009-07-16T16:42:19Z")

</div>

Hi all,

I have 37 different tables with each having its own sort column (sort  
implying that the main data of that table is sorted on that particular  
column). Each of these sort columns is different and unique.

To make things easier for myself I created an inheritance template where  
each of the 37 tables inherits from one templated controller and one  
templated model. They all use the same methods. So all 37  
controllers/models are basically empty and the inherited/templated  
controller and model contain the objects/methods.

I’m now creating a master table that needs to pull the data from each of  
those 37 tables and the sort columns, perform calculations, and then  
save the refined calculations back to itself (the master table).

I’m trying to figure out a proper way to setup the associations. All  
I’m doing is gathering data from those tables to calculate further  
values. My master data table belongs to my team model and the rest of  
the data tables belong to my team model. The current associations are:

class Team \< ActiveRecord::Base  
has\_many :master\_tables  
has\_many :inheritance\_templates  
end

class MasterTable \< ActiveRecord::Base  
belongs\_to :team  
end

class InheritanceTemplate \< ActiveRecord::Base  
self.abstract\_class = true  
belongs\_to :team  
end

class DataOne \< InheritanceTemplate  
end  
class DataTwo \< InheritanceTemplate  
end  
class DataThree \< InheritanceTemplate  
end

I’m not sure how the association should be setup between the Master  
Table model and the Inheritance Template model. They both belong to  
team.

Assume that I want to retrieve the following:

DataOne.colomnonedata  
DataTwo.columntwodata  
DataThree.columnthreedata

I could do:

@one = DataOne.find(:all)  
@two = DataTwo.find(:all)  
@three = DataThree.find(:all)

and point to a custom method that searches for the specific column and  
only returns results for that column but I figured there might be a  
better approach to all of this.

Any help would be appreciative…

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 16, 2009, 4:49pm UTC](https://www.ruby-forum.com/t/associations-over-37-different-tables-with-has-one/172199/2 "2009-07-16T16:49:58Z")

</div>

The reason why I’m asking this is I’m thinking that perhaps there’s “no  
association” at all between the master table and the other tables. The  
reason why I believe this is due to the fact that I’m pulling data from  
those tables, changing them, and saving refined data to the master  
table. Therefore, no columns from the tables I’m gathering data for  
belong to the master table.

However, the master table depends on the data from those tables in order  
to get its own numbers…

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 16, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/associations-over-37-different-tables-with-has-one/172199/3 "2009-07-16T17:31:36Z")

</div>

> No, help would be appreciated. You’d be appreciative of any help. 😃

LOL - thanks Marnen. You would not believe how often my fiance corrects  
me on my improper use of words… 🙂

I “appreciate” the help mate.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 16, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/associations-over-37-different-tables-with-has-one/172199/4 "2009-07-16T17:29:32Z")

</div>

Ã„lphÃ¤ BlÃ¼Ã« wrote:

> Hi all,
> 
> I have 37 different tables with each having its own sort column  
> […]  
> I’m now creating a master table that needs to pull the data from each of  
> those 37 tables and the sort columns, perform calculations, and then  
> save the refined calculations back to itself (the master table).
> 
> I’m trying to figure out a proper way to setup the associations. All  
> I’m doing is gathering data from those tables to calculate further  
> values. My master data table belongs to my team model and the rest of  
> the data tables belong to my team model.  
> […]  
> I’m not sure how the association should be setup between the Master  
> Table model and the Inheritance Template model. They both belong to  
> team.

It sounds like that’s all you need. I don’t see why InheritanceTemplate  
would come into the associations

[…]

> Any help would be appreciative…

No, help would be appreciated. You’d be appreciative of any help. 😃
