Accessing items when using Enumerable

Icenine Jon wrote:

I'm a RoR newbie and am trying to use the paginating_find plug-in (Welcome - 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})

2. 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).

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

#&lt;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"=>"", "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&gt;

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?

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 %>   <td>bookmark.test</td> <% 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.