Using acts_as_list on multiple columns
Asked Answered
Z

3

6

I have a possibly unique case where I need a model to have two differing orders depending on the model it is joined to. 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

So basically what I am trying to do is have a Book model which is part of two lists, one for the genre page it appears on, and one for the author page it appears on.

acts_as_list does not appear to support the use of 2 position columns as methods such as move_to_top does not allow you to specify which list to move to the top of.

Has anyone got any suggestions on how I could achieve this? Right now I am thinking I will have to create a join table such as books_genres which has a position column, but I am really not too keen on that as that requires a whole load of extra tables.

Zoochore answered 22/10, 2011 at 17:31 Comment(5)
Did you ever get this sorted? I've started a bounty as I need the same solutionComplex
Unfortunately not, couldn't find a solution, wouldn't mind that bounty though!Ashbey
I'm looking for the exact same feature...Logsdon
Same problem, 5 years later. I'll probably use ranked-model.Sells
Actually, this is now solved as of 0.7.6 https://mcmap.net/q/1792976/-using-acts_as_list-on-multiple-columnsWivinah
S
2

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

Add your scope in an array and use the ids as params...

belongs_to :website
belongs_to :page    
acts_as_list scope: [:website_id, :page_id]
Saxecoburggotha answered 29/7, 2016 at 21:22 Comment(0)
P
1

Acts as list not designed for multiple columns.plugin will almost rewrite if you want to use it in this way. But I think you can try 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 it does work. theoretically possible.

Pooch answered 12/1, 2012 at 7:29 Comment(1)
This works if you only need one kind of order per time you load a collection of books.Peroxy
R
1

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

For your example, you can do:

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
Requiem answered 6/10, 2014 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.