# Has\_and\_belongs\_to\_many Problem

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-problem/133876>\
**Category:** Rails\
**Created:** [April 4, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problem/133876 "2008-04-04T16:05:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [April 4, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problem/133876/1 "2008-04-04T16:05:45Z")

</div>

Hi all

I’m having a problem with has\_and\_belongs\_to\_many (habtm)…

Here is my setup:

tables: customers, customergroups and a join-table called  
customers\_customergroups

customers has an id as pk and so does customergroups. the join table  
has customer\_id and customergroup\_id (as well as an id)

both models include the habtm statement, one with :customergroups and  
the other with :customers

No I do the following:

customer = Customers.find(123)

at this point i would like to say: customer.customergroups.to\_xml.

The error message I get here is: uninitialized constant  
Customers::Customergroup

What am I doing wrong? Can anyone help?

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [April 4, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problem/133876/2 "2008-04-04T16:14:03Z")

</div>

Try changing the name of the join table to customergrous\_customers.  
Also with a straight habtm the join table doesn’t need an ID column,  
though I don’t know off hand if it breaks anything to have one.

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [April 4, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problem/133876/3 "2008-04-04T16:19:33Z")

</div>

I already switched both parts to customergroups\_customer, no effect. I  
now did remove the id column, because I don’t really need it, but  
still no luck.

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [April 4, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problem/133876/4 "2008-04-04T18:00:39Z")

</div>

On Fri, Apr 4, 2008 at 11:47 AM, dodgyboz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The join table should be called customergroups\_customers (notice the s  
> on the end of customers)

Actually I’m pretty sure that the default table name would be  
customers\_customergroups It get’s generated by the sort order of the  
(singular) class names and “customer” \< “customergroup”

## Of course it can be explicitly set using the :join\_table option on the :has\_and\_belongs\_to\_many relationship.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [April 5, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problem/133876/5 "2008-04-05T17:50:58Z")

</div>

Personally I’ve ditched habtm and the general drift in Rails  
development is to avoid it. If you use an explicit join table it’s a  
little easier to manage (IMO) and you gain the benefit of easily  
handling attributes on the association. Just to reduce the long names  
I’d use something like this:

Customer (as is)  
CustomerGroup (as is)  
GroupMembership (customer\_id, customer\_group\_id)

Customer  
has\_many :group\_memberships  
has\_many :customer\_groups, :through :group\_memberships

GroupMembership  
belongs\_to :customer  
belongs\_to :customer\_group

CustomerGroup  
has\_many :group\_memberships  
has\_many :customers, :through :group\_memberships

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [April 4, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problem/133876/6 "2008-04-04T17:48:51Z")

</div>

The join table should be called customergroups\_customers (notice the s  
on the end of customers) and should have 2 columns customer\_id and  
customergroup\_id their should be no primary key or any other column.

in your customer.rb file you should have as the second line  
has\_and\_belongs\_to\_many :customergroups

and in your customergroup.rb file you should have  
has\_and\_belongs\_to\_many :customers

(notice the models have singular names and the associations have  
pluralised names)
