# Which design pattern will work best with AR

**URL:** <https://www.ruby-forum.com/t/which-design-pattern-will-work-best-with-ar/171208>\
**Category:** Rails\
**Created:** [June 28, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/which-design-pattern-will-work-best-with-ar/171208 "2009-06-28T17:06:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [June 28, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/which-design-pattern-will-work-best-with-ar/171208/1 "2009-06-28T17:06:51Z")

</div>

Hello,

I’m writing a Rails application that will track people’s interests. I’d  
like to keep detailed information about each type of interest so I can’t  
just put everything in the interest table (the table would be just too  
wide)

User  
has\_many :interests

Interest  
belongs\_to :user  
has\_many :sports  
has\_many :books  
has\_many :tv\_shows  
etc., etc.

I was thinking that interest would have a type property that tells you  
which one of the relationships will have the data.

I know about single table inheritances and polymorphic relationships but  
can’t figure out how to apply them to come up with an elegant solution.

Thanks,

GP

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [June 28, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/which-design-pattern-will-work-best-with-ar/171208/2 "2009-06-28T17:26:13Z")

</div>

Grayson P. wrote:

> Hello,
> 
> I’m writing a Rails application that will track people’s interests. I’d  
> like to keep detailed information about each type of interest so I can’t  
> just put everything in the interest table (the table would be just too  
> wide)

There is absolutely nothing wrong with a wide table if that’s the best  
way to model your data. Don’t discard a design based on number of  
fields alone.

> User  
> has\_many :interests
> 
> Interest  
> belongs\_to :user  
> has\_many :sports  
> has\_many :books  
> has\_many :tv\_shows  
> etc., etc.

How can an Interest have many Sports? If you think about your naming  
scheme, you’ll see that it’s telling you that a Sport _is_ an Interest.  
So make Interest an abstract class (with subclasses Sport, Book, and  
TvShow), then either use STI or a polymorphic association to link each  
Interest subclass directly to User.

[…]

> Thanks,
> 
> GP

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [June 28, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/which-design-pattern-will-work-best-with-ar/171208/3 "2009-06-28T17:48:25Z")

</div>

2009/6/28 Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> way to model your data. Â Don’t discard a design based on number of
> 
> > Â has\_many Â :tv\_shows  
> > Â etc., etc.
> 
> How can an Interest have many Sports? Â If you think about your naming  
> scheme, you’ll see that it’s telling you that a Sport _is_ an Interest.  
> So make Interest an abstract class (with subclasses Sport, Book, and  
> TvShow), then either use STI or a polymorphic association to link each  
> Interest subclass directly to User.

Can an individual sport, book etc belong to many users? If so then  
you will need HABTM relationships between Users and Interests, if  
going down Marnen’s route (or between Interest and Sport if using your  
original concept).

Colin

Colin
