# HABTM Woes

**URL:** <https://www.ruby-forum.com/t/habtm-woes/68878>\
**Category:** Rails\
**Created:** [September 16, 2006, 6:05am UTC](https://www.ruby-forum.com/t/habtm-woes/68878 "2006-09-16T06:05:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derek\_Perrault](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Derek\_Perrault](https://www.ruby-forum.com/u/Derek_Perrault)\
**Post date:** [September 16, 2006, 6:05am UTC](https://www.ruby-forum.com/t/habtm-woes/68878/1 "2006-09-16T06:05:52Z")

</div>

I’m having a hell of a time getting HABTM working as advertised.

I’ve got my models: Person and Alias.

class Person \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :aliases  
end

class Alias \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :people  
end

I’ve built my sqlite3 database using the following migration:

class CreateAndAssociatePeopleAndAliases \< ActiveRecord::Migration  
def self.up  
create\_table :people do |t|  
t.column :first\_name, :string  
t.column :last\_name, :string  
end

```
create_table :aliases do |t|
  t.column :name, :string
end

create_table :people_aliases, {:id => false} do |t|
  t.column :person_id, :integer
  t.column :alias_id, :integer
end

```

end

def self.down  
drop\_table :people  
drop\_table :aliases  
drop\_table :people\_aliases  
end  
end

I pop open the console, create a person:

p = Person.new(:first\_name =\> ‘Buster’, :last\_name =\> ‘McThunderstick’)  
=\> #…  
p.aliases  
=\> []  
p.save  
p.id  
=\> 1

Now, if I attempt any action that comes within spitting distance of  
p.aliases, everything blows up. This includes p.aliases, p.destroy,  
etc.

Here’s the error:  
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid  
from  
./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/connection\_adapters/sqlite\_adapter.rb:266:in  
`table_structure' from ./script/../config/../config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/core_ext/object/misc.rb:23:in`returning’  
from  
./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/connection\_adapters/sqlite\_adapter.rb:265:in  
`table_structure' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:203:in`columns’  
from  
./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/associations/has\_and\_belongs\_to\_many\_association.rb:165:in  
`finding_with_ambigious_select?' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb:55:in`find’  
from  
./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/associations/association\_collection.rb:159:in  
`find_target' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:129:in`load\_target’  
from  
./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/associations/association\_proxy.rb:122:in  
`method_missing' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb:91:in`method\_missing’  
from /opt/local/lib/ruby/1.8/irb.rb:298:in `output_value' from /opt/local/lib/ruby/1.8/irb.rb:151:in`eval\_input’  
from /opt/local/lib/ruby/1.8/irb.rb:259:in `signal_status' from /opt/local/lib/ruby/1.8/irb.rb:147:in`eval\_input’  
from /opt/local/lib/ruby/1.8/irb.rb:146:in `eval_input' from /opt/local/lib/ruby/1.8/irb.rb:70:in`start’  
from /opt/local/lib/ruby/1.8/irb.rb:69:in `start’  
from /opt/local/bin/irb:13\>\>

I do have a workaround in place. If I include the join\_table option in  
my  
habtm calls, everything suddenly works.

But why do I have to add the join\_table option? I’m following the  
principle  
of convention plus redundant configuration.

This problem exists on Windows XP and OS X with both GemRails and Edge.  
Is  
this a known issue?  
And for that matter, what’s an ambigious\_select?

---

<div class="post-metadata">

**Author:** ![Derek\_Perrault](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Derek\_Perrault](https://www.ruby-forum.com/u/Derek_Perrault)\
**Post date:** [September 16, 2006, 6:23am UTC](https://www.ruby-forum.com/t/habtm-woes/68878/2 "2006-09-16T06:23:53Z")

</div>

Your join table needs to be called aliases\_people.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![Derek\_Perrault](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Derek\_Perrault](https://www.ruby-forum.com/u/Derek_Perrault)\
**Post date:** [September 16, 2006, 6:31am UTC](https://www.ruby-forum.com/t/habtm-woes/68878/3 "2006-09-16T06:31:54Z")

</div>

Thank you, Jonathan.

Why is the order important?

---

<div class="post-metadata">

**Author:** ![Derek\_Perrault](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Derek\_Perrault](https://www.ruby-forum.com/u/Derek_Perrault)\
**Post date:** [September 16, 2006, 6:37am UTC](https://www.ruby-forum.com/t/habtm-woes/68878/4 "2006-09-16T06:37:54Z")

</div>

Well, of course. Now that I realize order is important, Google throws  
the  
answer at me.

[http://blog.daveastels.com/articles/2006/08/19/has\_and\_belongs\_to\_many](http://blog.daveastels.com/articles/2006/08/19/has_and_belongs_to_many)

Alphabetic order. What a caveat to leave out of the documentation.

---

<div class="post-metadata">

**Author:** ![Derek\_Perrault](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Derek\_Perrault](https://www.ruby-forum.com/u/Derek_Perrault)\
**Post date:** [September 16, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/habtm-woes/68878/5 "2006-09-16T21:20:58Z")

</div>

You guys are awesome. My feet are thoroughly covered in saliva at this  
point, so I’ll just sign off with a hearty “thank you.” 🙂

---

<div class="post-metadata">

**Author:** ![Derek\_Perrault](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Derek\_Perrault](https://www.ruby-forum.com/u/Derek_Perrault)\
**Post date:** [September 16, 2006, 10:40am UTC](https://www.ruby-forum.com/t/habtm-woes/68878/6 "2006-09-16T10:40:09Z")

</div>

It’s in the documentation… the first paragraph in fact.

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

-Jonathan.
