# Problems with habtm

**URL:** <https://www.ruby-forum.com/t/problems-with-habtm/175218>\
**Category:** Rails\
**Created:** [September 9, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/problems-with-habtm/175218 "2009-09-09T20:29:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 9, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/problems-with-habtm/175218/1 "2009-09-09T20:29:59Z")

</div>

hi, i’m having some problems with habtm association, when i try to  
list Empresas form a Category, i get the association ID instead of the  
Empresa ID, i’m using sqlite3

> > paulo-geyers-macbook:100empresas paulo$ ./script/console  
> > Loading development environment (Rails 2.3.4)
> 
> > Category.find(:first).empresas.each {|e| puts e.id}  
> > 608  
> > 609  
> > =\> [#\<Empresa id: 608, name: “Fast Shop”, description: nil,  
> > created\_at: “2009-09-09 17:55:49”, updated\_at: “2009-09-09 17:55:49”\>,  
> > #\<Empresa id: 609, name: “Livraria Cultura”, description: nil,  
> > created\_at: “2009-09-09 17:55:49”, updated\_at: “2009-09-09 17:55:49”\>]
> 
> > Empresa.find(:all).each {|e| puts e.id}  
> > 54276017  
> > 1387109338  
> > =\> [#\<Empresa id: 54276017, name: “Livraria Cultura”, description:  
> > nil, created\_at: “2009-09-09 17:55:49”, updated\_at: “2009-09-09  
> > 17:55:49”\>, #\<Empresa id: 1387109338, name: “Fast Shop”, description:  
> > nil, created\_at: “2009-09-09 17:55:49”, updated\_at: “2009-09-09  
> > 17:55:49”\>]
> 
> >

here follows the model codes  
empresa.rb  
class Empresa \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :categories, :foreign\_key =\>  
‘empresa\_id’, :association\_foreign\_key =\> ‘category\_id’  
validates\_presence\_of :name  
acts\_as\_voteable  
end

category.rb  
class Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :empresas, :foreign\_key =\>  
‘category\_id’, :association\_foreign\_key =\> ‘empresa\_id’  
validates\_presence\_of :name  
end

i’ve tried to set the keys manually, but didn’t work too

thanks,  
Paulo

---

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 9, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/problems-with-habtm/175218/2 "2009-09-09T21:57:47Z")

</div>

it looks all wrong to me, and overconfigured too. check some  
tutorials.

Category habtm :empresas  
Empresa habtm :categories

CategoryEmpresa  
belongs\_to :category  
belongs\_to :empresa

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001836)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

---

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 10, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/problems-with-habtm/175218/3 "2009-09-10T19:05:14Z")

</div>

i didn’t create the model CategoriesEmpresas, and the table had the  
“id” field, that was the problem  
just another case of someone trying to use the framework without  
reading carefully the documentation

thanks Eric!

---

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 10, 2009, 8:39pm UTC](https://www.ruby-forum.com/t/problems-with-habtm/175218/4 "2009-09-10T20:39:27Z")

</div>

Paulo Geyer wrote:

> i didn’t create the model CategoriesEmpresas,

You don’t need that if you’re using habtm. You only need the join model  
if you’re using :through.

> and the table had the  
> “id” field, that was the problem

Yes, that apparently gets in the way.

> just another case of someone trying to use the framework without  
> reading carefully the documentation

🙂

> thanks Eric!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
