# HABTM Blank Join table

**URL:** <https://www.ruby-forum.com/t/habtm-blank-join-table/188778>\
**Category:** Rails\
**Created:** [May 18, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/habtm-blank-join-table/188778 "2010-05-18T12:23:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [May 18, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/habtm-blank-join-table/188778/1 "2010-05-18T12:23:46Z")

</div>

Based of the declaritive\_authorization railscast I’ve set up a roles  
table containing 3 role types. A user table and a roles\_users table.  
No matter what I do the join table is always empty.

DB:  
create\_table “roles”, :force =\> true do |t|  
t.string “role\_type”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end

create\_table “roles\_users”, :id =\> false, :force =\> true do |t|  
t.integer “role\_id”  
t.integer “user\_id”  
end

add\_index “roles\_users”, [“role\_id”], :name =\>  
“index\_roles\_users\_on\_role\_id”  
add\_index “roles\_users”, [“user\_id”], :name =\>  
“index\_roles\_users\_on\_user\_id”

create\_table “users”, :force =\> true do |t|  
t.string “first\_name”, :null =\> false  
t.string “middle\_name”  
t.string “last\_name”, :null =\> false  
t.string “email”, :null =\> false  
t.string “dealer\_id”, :null =\> false  
…

role.rb model:  
class Role \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users

attr\_protected :role\_type  
end

user.rb model:  
class User \< ActiveRecord::Base  
acts\_as\_authentic do |c|  
c.logged\_in\_timeout = 10.minutes  
c.login\_field = :email  
end  
has\_and\_belongs\_to\_many :roles  
belongs\_to :dealer

def role\_symbols  
roles.map do |file|  
role.name\_underscore.to\_sym  
end  
end

attr\_accessible :email, :password, :password\_confirmation, :first\_name,  
:middle\_name, :last\_name

end

The user is being created here:  
@dealer = Dealer.new(params[:dealer])  
@dealer.save!  
@user = @dealer.users.create(params[:user].merge(:role\_id =\> 3)  
@dealer.addresses.create(params[:mailing\_address])

Every field is created without error except the join table is left  
blank always. If i call:  
@user.role.create it will successfully create a new role (with a blank  
name which is bad) and a join table row. Other then that the join  
table is always empty.

Any suggestions. I’ve run out of things to try.

cheers,  
brianp

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [May 18, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/habtm-blank-join-table/188778/2 "2010-05-18T14:50:08Z")

</div>

I dont use HABTM much myself, however I dont think this line will  
work:  
@user = @dealer.users.create(params[:user].merge(:role\_id =\> 3)

Try something like this instead:  
@dealer = Dealer.create(params[:dealer])  
@user = @dealer.users.new(params[:user])  
@user.roles \<\< Role.find(3)  
@user.save

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [May 18, 2010, 10:11pm UTC](https://www.ruby-forum.com/t/habtm-blank-join-table/188778/3 "2010-05-18T22:11:08Z")

</div>

Hey Sharagoz,

Thanks a lot. It worked exactly as desired. Now I was lead to believe  
that the habtm relationship would be created automagically when the  
user object is created. Why is it we had to explicitly define it like  
this?

Thanks a lot!  
-brianp

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [May 19, 2010, 10:30am UTC](https://www.ruby-forum.com/t/habtm-blank-join-table/188778/4 "2010-05-19T10:30:30Z")

</div>

If ActiveRecord were to know that it’s supposed to create an habtm  
association to another object simply because a foreign\_key is present  
in the attributes hash of the new object, then I think that would  
create a lot of overhead in the create method, and it’s a bit too much  
magic in my opinion. On every create it would need to check the  
attributes hash for \*\_id attributes, then check if any habtm  
associations matches that, and if so create the record in the join  
table. Or a reverse version of that where it checks for habtm  
relations first. It’s possible but that’s not how it has been  
implemented.
