# Association headaches

**URL:** <https://www.ruby-forum.com/t/association-headaches/116505>\
**Category:** Rails\
**Created:** [October 6, 2007, 7:20am UTC](https://www.ruby-forum.com/t/association-headaches/116505 "2007-10-06T07:20:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Zentgraf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@David\_Zentgraf](https://www.ruby-forum.com/u/David_Zentgraf)\
**Post date:** [October 6, 2007, 7:20am UTC](https://www.ruby-forum.com/t/association-headaches/116505/1 "2007-10-06T07:20:04Z")

</div>

Hi,

I’m trying to wrap my head around an association problem.

```
 create_table :events do |t|
   t.column :name, :text
   t.column :place, :text
 end

create_table :users do |t|
   t.column :name, :text
end

create_table :events_users do |t|
   t.column :event_id, :integer
   t.column :user_id, :integer
   t.column :user_role, :text
 end

```

I want to use :events\_users as a HABTM table to connect :events  
and :users like so:

class Event \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
end

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :events  
end

But I also want to be able to access the extra information of which  
role the user had during an event. I can’t see how to do that.  
has\_many :through doesn’t seem to offer a solution either, or does it?

Best,  
Dav

---

<div class="post-metadata">

**Author:** ![David\_Zentgraf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@David\_Zentgraf](https://www.ruby-forum.com/u/David_Zentgraf)\
**Post date:** [October 6, 2007, 8:24am UTC](https://www.ruby-forum.com/t/association-headaches/116505/2 "2007-10-06T08:24:19Z")

</div>

So I figure I might create a new class inheriting from User and add  
some extra variables like:

class Participant \< User  
@role = …  
end

But how can I populate a single extra variable this way?  
Any suggestions please, still trying to figure Rails out…

Chrs,  
Dav

---

<div class="post-metadata">

**Author:** ![David\_Zentgraf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@David\_Zentgraf](https://www.ruby-forum.com/u/David_Zentgraf)\
**Post date:** [October 6, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/association-headaches/116505/3 "2007-10-06T12:47:20Z")

</div>

On 6. Oct 2007, at 14:19, David Zentgraf wrote:

> ```
> t.column :event_id, :integer
> t.column :user_id, :integer
> t.column :user_role, :text
> end
> 
> ```

I got my HABTM association between users and events going so far, I  
can even retrieve the records from the database including the extra  
information from the HABTM table.

event.users #=\> \<User @attributes={“name”=\>“Dav”, “user\_role”=\>“Host”}

> etc…

The user\_role being selected with the rest of the user attributes  
seems to be a side-effect of ActiveRecord selecting \* from all tables  
when retrieving HABTM associations. Good so far, but how can I get  
the user\_role value _into_ the table? I guess I could specify a  
custom :insert\_sql string in the model, but that seems very messy and  
additionally I wouldn’t know how to hand the attribute to ActiveRecord.

event.users.create(:name =\> “foo”, :user\_role =\> “Guest”)  
certainly wouldn’t work…

Any suggestions please!  
Chrs,  
Dav

---

<div class="post-metadata">

**Author:** ![David\_Zentgraf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@David\_Zentgraf](https://www.ruby-forum.com/u/David_Zentgraf)\
**Post date:** [October 6, 2007, 12:54pm UTC](https://www.ruby-forum.com/t/association-headaches/116505/4 "2007-10-06T12:54:20Z")

</div>

You need to use has\_many :through.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![David\_Zentgraf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@David\_Zentgraf](https://www.ruby-forum.com/u/David_Zentgraf)\
**Post date:** [October 6, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/association-headaches/116505/5 "2007-10-06T14:07:28Z")

</div>

I see! It actually works now. 🙂  
Thanks!
