Using acts_as_list on multiple columns

2.9k views Asked by At

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.

3

There are 3 answers

1
Ankun On

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.

0
nnattawat On

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
0
dsmithco On

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]