# 3 column :hbtm?

**URL:** <https://www.ruby-forum.com/t/3-column-hbtm/172693>\
**Category:** Rails\
**Created:** [July 25, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/3-column-hbtm/172693 "2009-07-25T21:17:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [July 25, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/3-column-hbtm/172693/1 "2009-07-25T21:17:56Z")

</div>

A Song :has and belongs to many Playlists.  
I want to store additional information about a song in a given  
playlist, i.e. the number of request of this song in a playlist.

Here is an example

Playlist a:  
|Song A|5 requests|  
|Song B|3 requests|

Playlist b:  
|Song A|2 requests|  
|Song B|4 requests|

In SQL I would add an extra column :number\_of\_requests to the  
songs\_playlists table.  
But how do I do it with pure ActiveRecord?

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [July 25, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/3-column-hbtm/172693/2 "2009-07-25T21:21:56Z")

</div>

Gleb M. wrote:

> A Song :has and belongs to many Playlists.  
> I want to store additional information about a song in a given  
> playlist

Then you’ll need has\_many :through. HABTM doesn’t deal with extra  
fields in the join table.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [July 25, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/3-column-hbtm/172693/3 "2009-07-25T21:30:49Z")

</div>

2009/7/25 Glex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Playlist b:  
> |Song A|2 requests|  
> |Song B|4 requests|
> 
> In SQL I would add an extra column :number\_of\_requests to the  
> songs\_playlists table.  
> But how do I do it with pure ActiveRecord?

You can make the join table be a fully fledged ActiveRecord model (it  
will need an id column) and add the new columns to it. Then use  
has\_many :through rather then habtm

Colin

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [July 25, 2009, 9:39pm UTC](https://www.ruby-forum.com/t/3-column-hbtm/172693/4 "2009-07-25T21:39:36Z")

</div>

2009/7/25 Glex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> >

Have a look at the rails guides at [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)  
particularly the one on ActiveRecord Associations (assuming you have  
already looked at the Getting Started one). Google will provide lots  
of help too.

Colin

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [July 25, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/3-column-hbtm/172693/5 "2009-07-25T21:33:10Z")

</div>

On Jul 25, 9:21 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> ## Marnen Laibow-Koserhttp://www.marnen.org removed\_email\_address@domain.invalid
> 
> Posted viahttp://www.ruby-forum.com/.

How would I do this? I am new to Rails.  
:has\_one request\_count :through =\> “what do i put here? where do I  
store it?”

Thank you

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [July 26, 2009, 4:15am UTC](https://www.ruby-forum.com/t/3-column-hbtm/172693/6 "2009-07-26T04:15:49Z")

</div>

Thank you, everyone, I made it work through :through 🙂
