Getting 22 elements

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.

Check out the documentation on Pagination

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…

On 5/31/06, Vladislav G. [email protected] 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

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

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-
enumerable-group_by-and-array-in_groups_of
http://dev.rubyonrails.org/changeset/4345

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

http://redhanded.hobix.com/bits/matchingIntoMultipleAssignment.html


Michael D.
http://www.mdaines.com

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”?

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