SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Los Angeles
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing items when using Enumerable

    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:

    1. In my controller, I'm making a call to get the data from the model using pagination:
    Code:
    @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:
    Code:
    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:
    Code:
    #<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>
    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!

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post your view code?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Los Angeles
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, here's a snippet of it...let me know if you need more:

    Code:
    <% for bookmark in @bookmarks %>
      <tr>
        <td><a href="<%= h bookmark.url -%>"><%= h bookmark.title -%></a></td>
        <td><%= h bookmark.description %></td>
        <td><%= h bookmark.test %></td>
    <% end %>
    Everything comes out except bookmark.test which I'm thinking is because it's not part of @attributes in the Enumerator listed above. Am I accessing it wrong?

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like you haven't set @test to anything in that example.

    Try:
    Code:
    <&#37; for bookmark in @bookmarks %>
      <% bookmark.test = rand %>
      <tr>
        <td><a href="<%= h bookmark.url -%>"><%= h bookmark.title -%></a></td>
        <td><%= h bookmark.description %></td>
        <td><%= h bookmark.test %></td>
    <% end %>
    "test" isn't going to get saved for future requests because it is just an instance variable, there's nowhere in the database to store it.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Los Angeles
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks samsm. But above, in my model, I put "attr_accessor :test". If I store a value in bookmark.test in the controller, won't the value get passed through to the view? I don't understand why when I set the value to test, I see it in the @bookmarks variable but I can't access it:

    Code:
    #<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>

  6. #6
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, my bad, totally missed that step.

    Interesting, I don't know what is up.

    ActiveRecord has a private method called "test", you might try test2 or foobar or something and see if the problem persists.
    Last edited by samsm; Jul 3, 2007 at 19:04. Reason: grammar
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Los Angeles
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, same thing with test2 and foobar . Anyone else have any suggestions?

    UPDATE
    Okay, so I just figured out that if I use a second variable @bookmarks2 and assign it using @bookmarks2 = @bookmarks.to_a (converting the PagingEnumerator to an array) I can then assign @bookmarks2.test2 and access it successfully in the view. The object itself looks identical though:

    Code:
    [#<Bookmark:0x3679a8c @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"}, @num_duplicates=1, @test2="test">
    Any idea what's going on here?
    Last edited by IceNineJon; Jul 3, 2007 at 17:31. Reason: More information added

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are you going to put in the test attribute? It makes more sense to put this in your model, so you can do:

    Code:
    class User < ActiveRecord::Base
      def visible_bookmarks
         # return the things here
      end
    end
    
    class Bookmark < ActiveRecord::Base
      def foo
         "test"
      end
    end
    
    @bookmarks = @user.visible_bookmarks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •