I need to sort a collection for formtastic by their state, wether they are selected or not.
My setup looks similar to the following
# user.rb
class User
has_and_belongs_to_many :colours
end
´
# colour.rb
class Colour
has_and_belongs_to_many :users
scope :ordered_for, lambda { |user|
all.sort_by { |s| include? user ? 1 : 0 }
}
end
´
# _form.html.haml
= semantic_form_for(@user) do |f|
= f.input :colours, :as => :check_boxes, :collection => Colour.ordered_for(current_user)
ordered_for
returns a well sorted Arr开发者_StackOverflow社区ay. In fact formtastic destroys this order afterwards and sorts the colours by id
.
What's a good approach to supres that behaviour? Is there any smarter way to achieve an order by selection?
Thanks in advance!
Please try:
Colour.ordered_for(current_user).all
精彩评论