# Attributes on join model

**URL:** <https://www.ruby-forum.com/t/attributes-on-join-model/152001>\
**Category:** Rails\
**Created:** [October 28, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/attributes-on-join-model/152001 "2008-10-28T20:53:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahoward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@ahoward](https://www.ruby-forum.com/u/ahoward)\
**Post date:** [October 28, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/attributes-on-join-model/152001/1 "2008-10-28T20:53:44Z")

</div>

given a setup like

class Parent  
has\_many :child\_parents  
has\_many :children, :through =\> child\_parents  
end

and assuming i’d like to store the style of relationship on the  
child\_parents table, for example

create table child\_parents(  
parent\_id  
child\_id  
is\_adopted  
)

in otherwords, ‘is\_adopted’ is an attribute of the link between child  
and parent, not of the child or parent themselves, how can one access  
this simply?

child = parent.children.first  
p child.adopted?

is obviously desirable and used to be possible with HABTM  
associations. but that’s deprecated… however there seems to be no  
easy way to accomplish something similar with full blown join models.

cheers.

---

<div class="post-metadata">

**Author:** ![ahoward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@ahoward](https://www.ruby-forum.com/u/ahoward)\
**Post date:** [October 28, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/attributes-on-join-model/152001/2 "2008-10-28T22:03:48Z")

</div>

On 28 Pa¼, 20:53, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> create table child\_parents(  
> p child.adopted?
> 
> is obviously desirable and used to be possible with HABTM  
> associations. but that’s deprecated… however there seems to be no  
> easy way to accomplish something similar with full blown join models.
> 
> cheers.

[http://wiki.rubyonrails.org/rails/pages/has\_and\_belongs\_to\_many](http://wiki.rubyonrails.org/rails/pages/has_and_belongs_to_many)

“…  
has\_and\_belongs\_to\_many is great for what it does, but it is only  
adequate for simple many-to-many relationships. If your intermediary  
table needs to track additional data, then you may instead want to use  
ThroughAssociations instead.  
…”

Cheers

---

<div class="post-metadata">

**Author:** ![ahoward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@ahoward](https://www.ruby-forum.com/u/ahoward)\
**Post date:** [October 28, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/attributes-on-join-model/152001/3 "2008-10-28T22:39:00Z")

</div>

yeah i’m aware of all that - the issue is that using HM:T give no  
simple method of getting back at the _particular_ model/row used for  
the join. of course there can by more that one, but it’s also the  
case that for any given pair of related objects there is exactly one  
row in the join model making that relationship and AR provides no  
handle on this record, which makes decorating it rather tricky.

---

<div class="post-metadata">

**Author:** ![ahoward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@ahoward](https://www.ruby-forum.com/u/ahoward)\
**Post date:** [October 29, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/attributes-on-join-model/152001/4 "2008-10-29T12:13:47Z")

</div>

ara.t.howard wrote:

> create table child\_parents(  
> p child.adopted?  
> cp = parent.child\_parents.first  
> child = cp.child  
> p cp.adopted?

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
