Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing results from an activerecord query into two objects

Is there a rails-like way to divide results from an activerecord query? For example, I did @results = Items.find(:all), but I want the top half of items from @results to appear in a line item under <ul class="part1">, and the other half of them to appear under <ul class="part2">.

<ul class="part1">
    <li><a href="#">result["name"]</a></li>
</ul>

<ul class="part2">
    <li><a href="#">resultpart2["name"]</a></li>
</ul>

thanks in advance!

like image 507
Jess Avatar asked Jan 20 '26 08:01

Jess


1 Answers

You can use the in_groups method from ActiveSupport:

@grouped_results = @results.in_groups(2)

and iterate over @grouped_results[0] for part1 and @grouped_results[1] for part2.

like image 135
Greg Campbell Avatar answered Jan 22 '26 23:01

Greg Campbell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!