Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using acts_as_list on multiple columns

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.

like image 570
Matthew O'Riordan Avatar asked Oct 22 '11 17:10

Matthew O'Riordan


2 Answers

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]
like image 136
dsmithco Avatar answered Oct 18 '22 11:10

dsmithco


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.

like image 1
Ankun Avatar answered Oct 18 '22 12:10

Ankun