Forum: Ruby on Rails Getting 22 elements

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Vladislav G. (Guest)
on 2006-05-31 14:58
Hello,

I've done
===
class HomeController < ApplicationController
  def index
    @country = Country.find(:all, :include => "cities")
  end
end
===
And rhtml is:

===
    <% @country.each do |country| %>
    <h1><%= country.name %></h1>
      <% country.cities.each do |city| %>
        - <%= link_to city.name, :action => "city/view", :id => city.id
%></li>
      <% end %>
    <% end %>
===
I have over 200 _counties_. How can I print counties #0-22, counties
#23-44 etc (with cities inside).

Thanks.
Carl-Johan K. (Guest)
on 2006-05-31 15:03
(Received via mailing list)
Check out the documentation on Pagination
Vladislav G. (Guest)
on 2006-05-31 15:06
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...
Michael D. (Guest)
on 2006-05-31 23:09
(Received via mailing list)
> 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
Wilson B. (Guest)
on 2006-05-31 23:09
(Received via mailing list)
On 5/31/06, Vladislav G. <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/...
Devin M. (Guest)
on 2006-06-02 04:41
(Received via mailing list)
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"?
Michael D. (Guest)
on 2006-06-02 06:53
(Received via mailing list)
> 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/...
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/matchingIntoMultip...


--
Michael D.
http://www.mdaines.com
Vladislav G. (Guest)
on 2006-06-03 17:55
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 %></li>
            <% end %>
            </ul>
          <% 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

......
This topic is locked and can not be replied to.