# Do I require a polymorphic link?

**URL:** <https://www.ruby-forum.com/t/do-i-require-a-polymorphic-link/152328>\
**Category:** Rails\
**Created:** [November 2, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/do-i-require-a-polymorphic-link/152328 "2008-11-02T14:44:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nytrex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a3d4f5/32.png) [@nytrex](https://www.ruby-forum.com/u/nytrex)\
**Post date:** [November 2, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/do-i-require-a-polymorphic-link/152328/1 "2008-11-02T14:44:43Z")

</div>

Hi there.  
I’m very new to RoR, and I’m writing my first ‘web app’ and already I  
think I need a one-to-many polymorphic join. The reason I’m writing  
this is because it is causing me a real headache. After sitting at my  
pc for what seems like 4 four solid days, I’m not getting anywhere.

Here’s the scenario;  
A user profile photo will be displayed based on which profile he/she  
has selected. There are three different types.  
a) Monthly photo b) Daily photo c) Specific date in year. Each of  
these are all different tables because they hold slightly different  
types of data.

So ‘Monthly’ would have at most 12 rows per user, whereas ‘daily  
photos’ and ‘specific date’ tables would have an unlimited amount of  
rows of data assigned to a user.

Does anyone know how best to go about to achieve this? I’ve tried  
using the example of the polymorphic join in AWDR V2, but it seems to  
be a one 2 one relationship. I can’t seem to get the One 2 many to  
work at all.

I suppose my question is this; Can anyone help with One to Many  
polymorpphic joins before the last of my hair falls out!

---

<div class="post-metadata">

**Author:** ![nytrex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a3d4f5/32.png) [@nytrex](https://www.ruby-forum.com/u/nytrex)\
**Post date:** [November 3, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/do-i-require-a-polymorphic-link/152328/2 "2008-11-03T12:21:13Z")

</div>

That’s no polymorphic relation at all.  
In a polymorphic relation would mean, that the table  
contains one and the same kind of data for a set of  
other tables.  
Say you have client and admin tables and want all of them  
to have an address, then address would be polymorphic,  
assigned either to a client or address.

In your case the user is the central table and the other three  
relate to it by simply having an user\_id field.

user has\_many monthly\_photos  
user has\_many daily\_photos  
user has\_many date\_photos

daily\_photos belongs\_to :users  
monthly\_photos belongs\_to :users  
date\_photos belongs\_to :users

You could enhance that code by using a single photo  
table with a kind\_of field, that’s saying if it’s daily,  
monthly or fixed date and using a use\_at date field,  
interpreting it depending on kind\_of.  
Or use “Single table inheritance” (explained in Rails API docs  
ActiveRecord::Base)  
But I wouldn’t do that for such a small piece of functionality.
