How to get Rails' acts_as_list to add new items to the beginning of a list?
Asked Answered
F

2

6

I have a model integrated with acts_as_list. However, by default new items created get added to the end of the list. Is there a default way to add new items to the beginning?

Thanks!

Fantom answered 22/8, 2012 at 22:26 Comment(0)
G
10

Try using the add_new_at configuration option:

acts_as_list scope: :todo, add_new_at: :top
Giorgio answered 12/8, 2013 at 21:52 Comment(2)
This is the best answer. I deleted my answer about the column default method as it no longer works.Lisbethlisbon
@yuval you should accept this answer as it's still valid, and is the official method.Lisbethlisbon
A
4

You can change the way the list items are displayed to begin with. Reversing the order of todo_items effectively accomplishes that.

has_many :todo_items, :order => "position ASC"

Otherwise, perhaps you can set the position column on the items manually in an after_create.

Analgesia answered 22/8, 2012 at 22:35 Comment(1)
This might break under the assumption that the OP has some kind of drag and drop reordering in their app. This would attempt to reorder all items according to their index position in the array of ids. Just something to be aware of.Lisbethlisbon

© 2022 - 2024 — McMap. All rights reserved.