Forum: Ruby on Rails Object attributes through habtm

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Wouter d. (Guest)
on 2009-05-14 17:57
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
Frederick C. (Guest)
on 2009-05-14 19:37
(Received via mailing list)
On May 14, 2:57 pm, Wouter de Bie <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
Wouter d. (Guest)
on 2009-05-14 19:56
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.
This topic is locked and can not be replied to.