# Acts\_as\_list and a Join table?

**URL:** <https://www.ruby-forum.com/t/acts-as-list-and-a-join-table/110258>\
**Category:** Rails\
**Created:** [July 24, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/acts-as-list-and-a-join-table/110258 "2007-07-24T18:52:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [July 24, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/acts-as-list-and-a-join-table/110258/1 "2007-07-24T18:52:20Z")

</div>

Hi,

I ve got two model, Video and Pack… My Pack model can have multiple  
Video, and a Video belongs to multiple Pack…So i m doing a join  
table, Video\_Pack…  
So my Models looks like :

Video :  
ID  
Name  
etc.

Pack :  
ID  
Name  
etc.

Video\_Pack  
Video\_id  
Pack\_video

I would like to be able to order the video in a Pack… so i guess the  
best solution is to use the acts\_as\_lists plugin… This way i will  
have to use it on the join table, Video\_Pack… so i’ve to add to my  
Video\_Pack model, the column Position… right?

Right now all of that is in my head, and i ve no idea if i m right or  
maybe 100% wrong… so if you could give me an hand i ll appreciate a  
lot,

Thanks in advance,  
Guillaume.

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [July 24, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/acts-as-list-and-a-join-table/110258/2 "2007-07-24T20:16:40Z")

</div>

Differenthink wrote:

> Hi,
> 
> I ve got two model, Video and Pack… My Pack model can have multiple  
> Video, and a Video belongs to multiple Pack…So i m doing a join  
> table, Video\_Pack…  
> So my Models looks like :
> 
> Video :  
> ID  
> Name  
> etc.
> 
> Pack :  
> ID  
> Name  
> etc.
> 
> Video\_Pack  
> Video\_id  
> Pack\_video
> 
> I would like to be able to order the video in a Pack… so i guess the  
> best solution is to use the acts\_as\_lists plugin… This way i will  
> have to use it on the join table, Video\_Pack… so i’ve to add to my  
> Video\_Pack model, the column Position… right?
> 
> Right now all of that is in my head, and i ve no idea if i m right or  
> maybe 100% wrong… so if you could give me an hand i ll appreciate a  
> lot,
> 
> Thanks in advance,  
> Guillaume.

This may not really answer your question, but the join table should be  
named with the two components in alphabetical order, lower case, and  
pluralised, so in your case the join table should be called

packs\_videos  
and have  
pack\_id, video\_id

Also i think it’s better to keep all of your id fields in lower case -  
id, video\_id, pack\_id : that’s what rails expects so you’ll have to do  
less configuration stuff.

In the model, you need to use the “has\_and\_belongs\_to\_many”  
relationship, ie

class Video \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :packs  
…  
end

class Pack \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :videos  
…  
end

Once you’ve got this, if you want all the videos from a pack (which you  
have as @pack for example) you should be able to simply say ask for  
@pack.videos.sort (i think) for a sorted view of all the videos that  
belong to that pack.

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [July 26, 2007, 12:45am UTC](https://www.ruby-forum.com/t/acts-as-list-and-a-join-table/110258/3 "2007-07-26T00:45:51Z")

</div>

Thanks for the tips,  
anyone for the my acts as list question/problem ?

On Jul 24, 8:16 pm, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/acts-as-list-and-a-join-table/110258/4 "2007-07-26T00:46:18Z")

</div>

By the way Max, i m using more that the ID in my join table so habtm  
is not working… i m using has\_many with the through parameters… so  
i can name my join table as i want…

On Jul 25, 12:05 am, Differenthink [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
