# Object attributes through habtm

**URL:** <https://www.ruby-forum.com/t/object-attributes-through-habtm/167972>\
**Category:** Rails\
**Created:** [May 14, 2009, 3:57pm UTC](https://www.ruby-forum.com/t/object-attributes-through-habtm/167972 "2009-05-14T15:57:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [May 14, 2009, 3:57pm UTC](https://www.ruby-forum.com/t/object-attributes-through-habtm/167972/1 "2009-05-14T15:57:01Z")

</div>

Hi all,

I have some “strange” behavior going on in one of my Rails apps.  
I have two models: CustomerCategory and PaymentType. Both have a HABTM  
relationship.

If I want to know the attributes of a PaymentType, I do  
PaymentType.find(1).attributes. This nicely returns a hash with all the  
attribute names and values (let’s say {“name” =\> “mastercard”}).

The strange thing that happens however is that when I get the payment  
type through a customer category, suddenly payment\_type\_id is added as  
an attribute to the payment type object. So  
CustomerCategory.find(1).payment\_types[0].attributes returns  
{“payment\_type\_id” =\> “1”, “name” =\> “mastercard”}.

Is this a Rails feature or a bug?

Regards,

Wouter

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [May 14, 2009, 5:37pm UTC](https://www.ruby-forum.com/t/object-attributes-through-habtm/167972/2 "2009-05-14T17:37:42Z")

</div>

On May 14, 2:57 pm, Wouter de Bie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The strange thing that happens however is that when I get the payment  
> type through a customer category, suddenly payment\_type\_id is added as  
> an attribute to the payment type object. So  
> CustomerCategory.find(1).payment\_types[0].attributes returns  
> {“payment\_type\_id” =\> “1”, “name” =\> “mastercard”}.
> 
> Is this a Rails feature or a bug?

It’s a semi deprecated feature: getting attributes from the join table  
automatically. The trend has since been to make that join table a  
model in its own right.

Fred

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [May 14, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/object-attributes-through-habtm/167972/3 "2009-05-14T17:56:09Z")

</div>

Frederick C. wrote:

> It’s a semi deprecated feature: getting attributes from the join table  
> automatically. The trend has since been to make that join table a  
> model in its own right.
> 
> Fred

I know that I can use a has\_many :through, but sometimes habtm is better  
in my application, since it doesn’t clutter my project so much with  
models I don’t use, except for the relationship. For relations that  
require more functionality, I use has\_many :through.
