In my controller I'm getting all Extras grouped by Category:
def index
@categories = Extra.all.group_by(&:category)
end
The result is something like an Array of Hashes:
{#<Category id:1, sort:2> => [#<Extra id:1>,#<Extra id:2],
#<Category id:2, sort: 1> => [#<Extra id:3>,#<Extra id:4>]}
I want to sort by category "sort" column instead of id, which should look like this:
{#<Category id:2, sort:1> => [#<Extra id:3>,#<Extra id:4],
#<Category id:1, sort: 2> => [#<Extra id:1>,#<Extra id:2>]}
When I try:
def index
@categories = Extra.all.group_by(&:category).sort_by{|s| s[:sort]}
end
I get "no implicit conversion of Symbol into Integer". Is that because I used a symbol in the "sort_by"?
Try this, group_by
will return you hash
and you are trying to call [:sort]
on an Array of [#<Category id:2, sort:1>, [#<Extra id:3>,#<Extra id:4]]
You should take them in two variables |key, value|
and the key will be category
def index
@categories = Extra.all.group_by(&:category).sort_by{|category, _extras| category.sort }
end
Use the order
method to do the sorting at the database:
@categories = Extra.order(sort: :asc).group_by(&:category)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With