Using act_as_list in multiple columns

I have, perhaps, a unique case when I need a model for two different orders, depending on the model to which it is attached. An example as follows:

class Book acts_as_list :column => :genre, :scope => :genre acts_as_list :column => :author, :scope => :author belongs_to :genre belongs_to :author end 

Thus, basically I'm trying to make a model of a book, which is part of two lists, one for the genre page on which it appears, and one for the author’s page on which it appears.

acts_as_list does not seem to support the use of two position columns, since methods such as move_to_top do not allow you to specify which list should be moved to the beginning.

Does anyone have any suggestions on how I can achieve this? Now I think I will have to create a join table, such as books_genres , which has a position column, but I'm really not too keen on this, since it requires a whole set of additional tables.

+6
source share
3 answers

Old post, but I hope this helps. This works with act_as_list 0.7.6 at least ...

Add your scope to the array and use identifiers as parameters ...

 belongs_to :website belongs_to :page acts_as_list scope: [:website_id, :page_id] 
+2
source

Acts as a list, not intended for multiple columns .plugin will almost overwrite if you want to use it that way. But I think you can try to do this.

 class Book belongs_to :genre belongs_to :author end class GenreBook < Book acts_as_list :column => :genre, :scope => :genre end class AuthorBook < Book acts_as_list :column => :author, :scope => :author end 

not sure if it works. theoretically possible.

+1
source

Try another gem called ranked-model . It supports the case you mentioned by specifying the option: with_same. I tried it myself.

In your example, you can:

 class Book belongs_to :genre belongs_to :author ranks :within_genre, :with_same => :genre_id, :column => :genre ranks :within_author, :with_same => :author_id, :column => :author end 
+1
source

Source: https://habr.com/ru/post/899876/


All Articles