# Getting 22 elements

**URL:** <https://www.ruby-forum.com/t/getting-22-elements/62090>\
**Category:** Rails\
**Created:** [May 31, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/getting-22-elements/62090 "2006-05-31T12:58:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [May 31, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/getting-22-elements/62090/1 "2006-05-31T12:58:55Z")

</div>

Hello,

# I’ve done

# class HomeController \< ApplicationController def index @country = Country.find(:all, :include =\> “cities”) end end

And rhtml is:

# === \<% @country.each do |country| %\>

# \<%= country.name %\>
  
\<% country.cities.each do |city| %\>  
- \<%= link\_to city.name, :action =\> “city/view”, :id =\> city.id  
%\>  
\<% end %\>  
\<% end %\>

I have over 200 _counties_. How can I print counties #0-22, counties  
#23-44 etc (with cities inside).

Thanks.

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [May 31, 2006, 1:03pm UTC](https://www.ruby-forum.com/t/getting-22-elements/62090/2 "2006-05-31T13:03:59Z")

</div>

Check out the documentation on Pagination

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [May 31, 2006, 1:06pm UTC](https://www.ruby-forum.com/t/getting-22-elements/62090/3 "2006-05-31T13:06:16Z")

</div>

Carl-Johan K. wrote:

> Check out the documentation on Pagination

I need to show all these coutries on single page. It’s not a  
pagination…

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [May 31, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/getting-22-elements/62090/4 "2006-05-31T21:09:49Z")

</div>

On 5/31/06, Vladislav G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Carl-Johan K. wrote:
> 
> > Check out the documentation on Pagination
> 
> I need to show all these coutries on single page. It’s not a  
> pagination…

Check out ‘in\_groups\_of’:  
[http://weblog.rubyonrails.org/articles/2006/03/01/new-in-rails-enumerable-group\_by-and-array-in\_groups\_of](http://weblog.rubyonrails.org/articles/2006/03/01/new-in-rails-enumerable-group_by-and-array-in_groups_of)

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [May 31, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/getting-22-elements/62090/5 "2006-05-31T21:09:42Z")

</div>

> I have over 200 _counties_. How can I print counties #0-22, counties  
> #23-44 etc (with cities inside).

You could use Array#in\_groups\_of in Rails’ core extensions to do this:

\<% @countries.in\_groups\_of(22) do |group\_of\_countries| %\>  
\<% group\_of\_countries.each do |country| %\>  
\<%= country.name %\>  
\<% end %\>  
\<% end %\>

in\_groups\_of doesn’t appear to be in the online api documentation for  
some reason, so maybe you’ll have to look in

[your gems installation]/activesupport-[whatever version]/lib/  
active\_support/core\_ext/array.rb

–  
Michael D.  
[http://www.mdaines.com](http://www.mdaines.com)

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [June 2, 2006, 4:53am UTC](https://www.ruby-forum.com/t/getting-22-elements/62090/6 "2006-06-02T04:53:07Z")

</div>

> When they #:nodoc: a method like that, is that their way of saying,  
> “This is an internal thingamajigger, and not guaranteed to work the  
> same in the next version”?

I don’t think it’s supposed to be considered internal. It’s mentioned  
in the weblog, and the only change so far keeps the current  
functionality. (Now if you don’t pass a block it returns the grouped  
arrays instead of yielding them.) All in all, in\_groups\_of is pretty  
unsurprising, I think.

[http://weblog.rubyonrails.org/articles/2006/03/01/new-in-rails-](http://weblog.rubyonrails.org/articles/2006/03/01/new-in-rails-)  
enumerable-group\_by-and-array-in\_groups\_of  
[http://dev.rubyonrails.org/changeset/4345](http://dev.rubyonrails.org/changeset/4345)

You can always put this in your lib if you want:

[http://redhanded.hobix.com/bits/matchingIntoMultipleAssignment.html](http://redhanded.hobix.com/bits/matchingIntoMultipleAssignment.html)

–  
Michael D.  
[http://www.mdaines.com](http://www.mdaines.com)

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [June 2, 2006, 2:41am UTC](https://www.ruby-forum.com/t/getting-22-elements/62090/7 "2006-06-02T02:41:17Z")

</div>

Michael D. wrote:

> in\_groups\_of doesn’t appear to be in the online api documentation for  
> some reason

When they #:nodoc: a method like that, is that their way of saying,  
“This is an internal thingamajigger, and not guaranteed to work the same  
in the next version”?

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [June 3, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/getting-22-elements/62090/8 "2006-06-03T15:55:35Z")

</div>

Hm… I tried

```
    <% @topic.in_groups_of(3) do |group_of_topics| %>
      <% group_of_topics.each do |topic| %>
        <hr />
        <h2><%= topic.name %></h2>
        <ul>
        <% topic.categories.each do |category| %>
          <li><%= link_to category.name, :action => "view", :id => 

```

category.id %\>  
\<% end %\>  
  
\<% end %\>  
\<% end %\>

but I have

=You have a nil object when you didn’t expect it!  
=The error occured while evaluating nil.name

My controller is

def index  
@topic = Topic.find(:all, :include =\> “categories”)  
end

…
