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.