Forum: Ruby on Rails Accessing items when using Enumerable

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.
Dfa2d7f57b41607e705b8f54b36a44a1?d=identicon&s=25 Icenine Jon (iceninejon)
on 2007-07-04 02:43
I'm a RoR newbie and am trying to use the paginating_find plug-in
(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})

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:

#<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", "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.

Any help is much appreciated. Thanks!
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-07-04 07:50
(Received via mailing list)
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.
Dfa2d7f57b41607e705b8f54b36a44a1?d=identicon&s=25 Icenine Jon (iceninejon)
on 2007-07-04 08:44
Mark Reginald James 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 %>
  <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?
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-07-04 09:23
(Received via mailing list)
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.

--
We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.