A
A
Andrey Andreev2015-04-18 18:30:22
Ruby on Rails
Andrey Andreev, 2015-04-18 18:30:22

How to do has_and_belongs_to_many with sort?

Please tell me how to implement the following idea.
There are 3 models.
Video, Product and Product_group.
Product and Product_group have many Video. One Video can belong to 0 or more Product and/or Product_group, but that's not the problem, it's that the order of the video must be mutable.
For example, there are 5 Videos (id 1..5)
There is Product 1 to which videos 4,1,3 belong.
There is Product 2 to which videos 5.2 belong . There is a Product
Group to which videos 1.5 belong.
etc.
Partially solves my problem has_and_belongs_to_many and two joining tables
products_videos
t.integer "product_id"
t.integer "video_id"
and
product_groups_videos
t.integer "product_group_id"
t.integer "video_id"
However, I have no idea how else to add sorting here. You can add t.integer "position" to both tables, but how to change this value and how to sort by it?
Two separate models for linking with has_many :through do not want to be done, for the sake of one sorting column.
Please put me on the right path :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2015-04-18
@b0nn1e

Forget about has_and_belongs_to_many Use
has_many through instead:
Make ProductVideo model where position is

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question