# Relationships question

**URL:** <https://www.ruby-forum.com/t/relationships-question/127498>\
**Category:** Rails\
**Created:** [January 29, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/relationships-question/127498 "2008-01-29T20:43:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [January 29, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/relationships-question/127498/1 "2008-01-29T20:43:00Z")

</div>

Hi,

I have the following type of situation: three different models (X, Y,  
Z). There is no relationship between X and Y, but Y should have\_many  
of Z (although I’m not convinced Z should belong\_to Y, and it may even  
belong\_to both Y and X). Instances of Z are created for specific  
combinations of X and Y (ie, Z will likely have the fields x\_id and  
y\_id). Z will also contain a name that will be used as an identifier  
for an instance Y to an instance of X.

I’m not sure if I should simply have a has\_many/belongs\_to  
relationship between Y and Z, and then search through this subset for  
an instance of X (to identify the instance of Y as Z) or if there is a  
more eloquent way to achieve this.

Any thoughts/ideas would be appreciated.

Thanks,

Simon

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [January 29, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/relationships-question/127498/2 "2008-01-29T23:56:24Z")

</div>

X: has\_many :zs  
Y: has\_many :zs  
Z: belongs\_to :y, belongs\_to :x

On Jan 30, 2008 6:12 AM, Simon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [January 30, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/relationships-question/127498/3 "2008-01-30T19:37:41Z")

</div>

Since you want to specify Zs that correspond to an association between  
x and y it sounds like the best solution is to introduce a fourth  
model to capture the state of the association and then let it be the  
owner of the relationship to Z. The canonical example has been the  
publishing house that has Customers and Publications; the Subscription  
model is introduced to map the association between Customer and  
Publication.

In your scenario, it sounds like the following might work:

class X  
has\_many :xys  
has\_many :zs, :through=\>:xys  
end

class Y  
has\_many :xys  
has\_many :zs, :through=\>:xys  
end

class Z  
belongs\_to :xy  
end

class XY  
belongs\_to :x  
belongs\_to :y  
end

By doing this X and Y have a view into their relationship to one  
another and both can see the Zs they know about.
