# HABTM Woes

**URL:** <https://discuss.rubyonrails.org/t/habtm-woes/1699>\
**Category:** rubyonrails-talk\
**Created:** [September 16, 2006, 4:00am UTC](https://discuss.rubyonrails.org/t/habtm-woes/1699 "2006-09-16T04:00:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derek](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@Derek](https://discuss.rubyonrails.org/u/Derek)\
**Post date:** [September 16, 2006, 4:00am UTC](https://discuss.rubyonrails.org/t/habtm-woes/1699/1 "2006-09-16T04:00:49Z")

</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](http://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:** ![Jonathan5](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@Jonathan5](https://discuss.rubyonrails.org/u/Jonathan5)\
**Post date:** [September 16, 2006, 4:19am UTC](https://discuss.rubyonrails.org/t/habtm-woes/1699/2 "2006-09-16T04:19:14Z")

</div>

Your join table needs to be called aliases\_people.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![Derek](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@Derek](https://discuss.rubyonrails.org/u/Derek)\
**Post date:** [September 16, 2006, 4:26am UTC](https://discuss.rubyonrails.org/t/habtm-woes/1699/3 "2006-09-16T04:26:41Z")

</div>

Thank you, Jonathan.

Why is the order important?

---

<div class="post-metadata">

**Author:** ![Derek](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@Derek](https://discuss.rubyonrails.org/u/Derek)\
**Post date:** [September 16, 2006, 4:33am UTC](https://discuss.rubyonrails.org/t/habtm-woes/1699/4 "2006-09-16T04:33:56Z")

</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:** ![Jonathan5](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@Jonathan5](https://discuss.rubyonrails.org/u/Jonathan5)\
**Post date:** [September 16, 2006, 8:36am UTC](https://discuss.rubyonrails.org/t/habtm-woes/1699/5 "2006-09-16T08:36:18Z")

</div>

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

[http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000533](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000533)

-Jonathan.

---

<div class="post-metadata">

**Author:** ![Derek](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@Derek](https://discuss.rubyonrails.org/u/Derek)\
**Post date:** [September 16, 2006, 7:17pm UTC](https://discuss.rubyonrails.org/t/habtm-woes/1699/6 "2006-09-16T19:17:15Z")

</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.” 🙂
