hi guys & thx for the suggestions
1) slice: didnt know u can use that on "collections" too, so i give it a
try. the boundaries are being checked to not run into a nil situation

2) based on my snippet, insoect works, but if i want to address the
attribute like:

   <%=h  category.children[index].id %><br>

gives me:

Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id


not sure how to access or whats actually wrong....

thx again!



On Sun, May 9, 2010 at 4:35 AM, Michael Pavling <[email protected]> wrote:

> On 9 May 2010 02:17, tom <[email protected]> wrote:
> >        <% for index in 0 ... 5 %>
> >          <%=h  category.children[index].inspect %><br>
> >        <% end %>
>
> >> as u can see, in the for-loop i want for example show only 5 children
> > of that specific children. inspect gives me the correct values, but i
> > cant address them. whats the proper way here?
>
> First off, consider what will happen if your category has less than
> five children (although you're looping through six!); you'll get a nil
> value, and all the child.attribute stuff you'll want to do will break.
> So I'd suggest that instead of accessing by index, you iterate a slice
> of your collection:
>
> >        <% category.children.slice(0..4) do |child| %>
> >          <%=h  child.inspect %><br>
> >        <% end %>
>
> Then, what's the error you're getting when you replace "child.inspect"
> with "child.attribute"? (I have a feeling it might have been a "no
> method 'attribute' for nilclass...", which won't occur now you're
> iterating only existing children, but that's just a hunch ;-)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to