# Can't use habtm at all

**URL:** <https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081>\
**Category:** Rails\
**Created:** [November 24, 2006, 4:00am UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081 "2006-11-24T04:00:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 24, 2006, 4:00am UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081/1 "2006-11-24T04:00:36Z")

</div>

Railsers:

Please blame SQLite3, not my feeb capacity to read the Agile book, and  
all the blogs.

I want to do many-to-many between two tables. Classic relational stuff,  
right?

So my schema looks a little bit like this:

create\_table “users\_props”, :force =\> true do |t|  
t.column “prop\_id”, :integer  
t.column “user\_id”, :integer  
end

That links out to users and props tables, right? So their models  
contain the sacred habtm incantations:

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :props  
…

class Prop \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
…

All too obvious to post, right? And all straight out of the Daves’  
Agile book, right?

Now let’s put them in motion:

user.props \<\< prop

That produces a huge error message like this:

ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/sqlite\_adapter.rb:259:in  
`table_structure' /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/core_ext/object/misc.rb:23:in `returning’

I know how to fix that. I replace the syntactic sugar with this  
old-skool SQL:

```
        Prop.find_by_sql("""
                insert into users_props(user_id, prop_id)
                               values(#{user.id}, #{prop.id})
                """)

```

Okay. The record is probably there (I honestly didn’t check.) Now I  
try to read it out:

assert\_equal 1, quentin.props.count

Now check what _that_ gives:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table:  
props\_users: SELECT count(\*) AS count\_all FROM props INNER JOIN  
props\_users ON props.id = props\_users.prop\_id WHERE  
(props\_users.user\_id = 1 )

Why is it asking for props\_users when I have a users\_props?

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 24, 2006, 4:52am UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081/2 "2006-11-24T04:52:44Z")

</div>

You need to rename your users\_props table to props\_users; Rails  
expects the table names to be joined in alphabetical order, and  
“props” comes before “users” alphabetically.

Regards

Dave M.

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 24, 2006, 5:02am UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081/3 "2006-11-24T05:02:45Z")

</div>

On Nov 24, 2006, at 11:59 , Phlip wrote:

> Why is it asking for props\_users when I have a users\_props?

I believe ActiveRecord by default assumes join tables to be named  
with the joined table names in alphabetical order.

Michael G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 24, 2006, 5:12am UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081/4 "2006-11-24T05:12:46Z")

</div>

David M. wrote:

> You need to rename your users\_props table to props\_users; Rails  
> expects the table names to be joined in alphabetical order, and  
> “props” comes before “users” alphabetically.

Thaaanks.

And, yep, the Agile book says that.

They shouldn’t have put it above the paragraphs where anyone with \>5  
years  
experience coding Ruby would have started skimming!!!

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 24, 2006, 6:44am UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081/5 "2006-11-24T06:44:54Z")

</div>

Yep - I fell into this same trap the first time I tried writing a  
Rails app, and it took someone else to point it out to me… Now it’s  
your turn to carry on the tradition!

Regards

Dave M.

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 24, 2006, 9:59am UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081/6 "2006-11-24T09:59:09Z")

</div>

> And, yep, the Agile book says that.
> 
> They shouldn’t have put it above the paragraphs where anyone with \>5 years  
> experience coding Ruby would have started skimming!!!

It’s an excellent book, the only problem I had with it was that I had  
development experience and wanted to get straight into applying it.  
After stumbling through it once I then realised that reading it cover  
to cover before playing made much more sense as there is plenty of  
solid content later in the book (especially in the testing chapters)  
that cement things that looked like voodoo earlier.

Just my thoughts,

Glenn

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 24, 2006, 5:16am UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081/7 "2006-11-24T05:16:47Z")

</div>

David M. wrote:

> You need to rename your users\_props table to props\_users; Rails  
> expects the table names to be joined in alphabetical order, and  
> “props” comes before “users” alphabetically.

Oooh, one more wisecrack:

Well, I’m off to edit my schema.rb file, right below where it says not  
to  
edit it!

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 25, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081/8 "2006-11-25T14:07:02Z")

</div>

One other thing, you should add :id =\> false to create\_table:

create\_table “users\_props”, :force =\> true, :id =\> false do |t|  
t.column “prop\_id”, :integer  
t.column “user\_id”, :integer  
end

HABTM join tables should not have an ID column.

V/r

On 11/25/06, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hoo-yah. I take it with me whenever I have to go somewhere that’s  
> computer-hostile. Just avoid the pages I have already read too much!
> 
> –  
> Phlip  
> [Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!
> 
> >

–  
Email: removed\_email\_address@domain.invalid  
Cell: 808 782-5046  
Current Location: Melbourne, FL

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 25, 2006, 7:54am UTC](https://www.ruby-forum.com/t/cant-use-habtm-at-all/77081/9 "2006-11-25T07:54:26Z")

</div>

glenn wrote:

> It’s an excellent book, the only problem I had with it was that I had  
> development experience and wanted to get straight into applying it.  
> After stumbling through it once I then realised that reading it cover  
> to cover before playing made much more sense as there is plenty of  
> solid content later in the book (especially in the testing chapters)  
> that cement things that looked like voodoo earlier.

Hoo-yah. I take it with me whenever I have to go somewhere that’s  
computer-hostile. Just avoid the pages I have already read too much!

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!
