# Accessing items when using Enumerable

**URL:** <https://www.ruby-forum.com/t/accessing-items-when-using-enumerable/108493>\
**Category:** Rails\
**Created:** [July 4, 2007, 2:43am UTC](https://www.ruby-forum.com/t/accessing-items-when-using-enumerable/108493 "2007-07-04T02:43:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceninejon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@iceninejon](https://www.ruby-forum.com/u/iceninejon)\
**Post date:** [July 4, 2007, 2:43am UTC](https://www.ruby-forum.com/t/accessing-items-when-using-enumerable/108493/1 "2007-07-04T02:43:50Z")

</div>

I’m a RoR newbie and am trying to use the paginating\_find plug-in  
([Welcome cardboardrocket.com - BlueHost.com](http://cardboardrocket.com/pages/paginating_find)) for pagination.  
Everything works fine but after my .find() call, I want to add  
additional data to my object that’s not part of the data model (the  
attributes are however defined using attr\_accessor). However, it doesn’t  
seem to work. Can someone tell me what I’m doing wrong? Here’s what I’m  
trying:

bookmarks\_controller.rb  
@bookmarks = Bookmark.find(:all, :conditions =\> “private = ‘#{false}’ OR  
user\_id = ‘#{session[:user\_id]}’”, :order =\> “created\_at desc”, :group  
=\> “url”, :page =\> {:size =\> 20, :current =\> params[:page], :first =\>  
1})

1. I then try to add some additional data to @bookmarks:  
for bookmark in @bookmarks  
bookmark.test = “test”  
end

(in my Bookmark model, I have “attr\_accessor :test” since “test” is not  
part of the database model).

1. When I put a breakpoint in my view and using breakpointer, write out  
the @bookmarks variable, I get:

#\<PagingEnumerator:0x35222d8 @stop\_page=1, @size=10, @page\_count=1,  
@results=[#\<Bookmark:0x351fe98 @attributes={“updated\_at”=\>“2007-06-02  
18:02:46”, “title”=\>“Bluecross”, “private”=\>“0”,  
“url”=\>“[http://www.bluecross.com](http://www.bluecross.com)”, “id”=\>“11”, “description”=\>“Another  
health care company that my former employer used.”, “user\_id”=\>“1”,  
“created\_at”=\>“2007-06-02 18:02:46”}, @test=“test”,  
@num\_duplicates=1\>

Notice how there’s an @attributes in there which has all the data for my  
bookmark variable and then outside of it, is the @test. This obviously  
doesn’t work when I iterate through @bookmarks in my view (it only picks  
up what’s in the @attributes for each bookmark). How can I get it so the  
test attribute is included with the rest of the attributes? I asked  
someone on a blog and he replied “the data is stored in â€˜resultsâ€™  
variable within PagingEnumerator, hence you can write an accessor method  
to iterate through the collection and retain new values (for example,  
write a each\_set method).” but I don’t know how to do that.

Any help is much appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![iceninejon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@iceninejon](https://www.ruby-forum.com/u/iceninejon)\
**Post date:** [July 4, 2007, 7:50am UTC](https://www.ruby-forum.com/t/accessing-items-when-using-enumerable/108493/2 "2007-07-04T07:50:17Z")

</div>

Icenine Jon wrote:

> user\_id = ‘#{session[:user\_id]}’", :order =\> “created\_at desc”, :group
> 
> Notice how there’s an @attributes in there which has all the data for my  
> bookmark variable and then outside of it, is the @test. This obviously  
> doesn’t work when I iterate through @bookmarks in my view (it only picks  
> up what’s in the @attributes for each bookmark). How can I get it so the  
> test attribute is included with the rest of the attributes? I asked  
> someone on a blog and he replied “the data is stored in ‘results’  
> variable within PagingEnumerator, hence you can write an accessor method  
> to iterate through the collection and retain new values (for example,  
> write a each\_set method).” but I don’t know how to do that.

If you want to force test inside the @attributes hash you can  
do bookmark[:test] = “test”, but are you using some sort of  
scaffold that only allows AR attributes to be displayed in views?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![iceninejon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@iceninejon](https://www.ruby-forum.com/u/iceninejon)\
**Post date:** [July 4, 2007, 8:44am UTC](https://www.ruby-forum.com/t/accessing-items-when-using-enumerable/108493/3 "2007-07-04T08:44:37Z")

</div>

Mark Reginald J. wrote:

> Icenine Jon wrote:
> 
> > user\_id = ‘#{session[:user\_id]}’", :order =\> “created\_at desc”, :group
> > 
> > Notice how there’s an @attributes in there which has all the data for my  
> > bookmark variable and then outside of it, is the @test. This obviously  
> > doesn’t work when I iterate through @bookmarks in my view (it only picks  
> > up what’s in the @attributes for each bookmark). How can I get it so the  
> > test attribute is included with the rest of the attributes? I asked  
> > someone on a blog and he replied “the data is stored in ‘results’  
> > variable within PagingEnumerator, hence you can write an accessor method  
> > to iterate through the collection and retain new values (for example,  
> > write a each\_set method).” but I don’t know how to do that.
> 
> If you want to force test inside the @attributes hash you can  
> do bookmark[:test] = “test”, but are you using some sort of  
> scaffold that only allows AR attributes to be displayed in views?
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

Thanks for responding Mark. What’s an AR attribute (sorry, I’m not  
fully up on the terminology yet)? My view has:

\<% for bookmark in @bookmarks %\>

 bookmark.test \<% end %\>

In my controller, I created a new variable @bookmarks2 and set it to  
@bookmarks2 = @bookmarks.to\_a. When I do that and then loop through  
@bookmarks2 and adding bookmark2.test = “test” to each element and then  
run that through my view, it works fine (in other words, @bookmarks  
which is part of the enumerator won’t display it but @bookmarks2 which  
is a simple array will). Any ideas why?

---

<div class="post-metadata">

**Author:** ![iceninejon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@iceninejon](https://www.ruby-forum.com/u/iceninejon)\
**Post date:** [July 4, 2007, 9:23am UTC](https://www.ruby-forum.com/t/accessing-items-when-using-enumerable/108493/4 "2007-07-04T09:23:27Z")

</div>

Icenine Jon wrote:

> Thanks for responding Mark. What’s an AR attribute (sorry, I’m not  
> fully up on the terminology yet)? My view has:

ActiveRecord attribute, a database-backed attribute, as opposed to  
a memory-backed attr\_accessor Ruby attribute.

> \<% for bookmark in @bookmarks %\>
> 
> bookmark.test \<% end %\>
> 
> In my controller, I created a new variable @bookmarks2 and set it to  
> @bookmarks2 = @bookmarks.to\_a. When I do that and then loop through  
> @bookmarks2 and adding bookmark2.test = “test” to each element and then  
> run that through my view, it works fine (in other words, @bookmarks  
> which is part of the enumerator won’t display it but @bookmarks2 which  
> is a simple array will). Any ideas why?

I think what’s happening is that the paging\_find plugin retrieves  
the page of objects from the DB every time you use each/for to iterate  
over them. So you’re making the change to one set of bookmarks, and  
then retrieving a new unchanged set when you do your view iteration.  
Use of to\_a may be the best solution.

–  
We develop, watch us RoR, in numbers too big to ignore.
