Topic: Having trouble figuring out will_paginate and attachment_fu model

Hi everyone,

I'm trying to use the will_paginate plugin to paginate some athlete names and display their thumbnail image from the associated attachment_fu table together.

While this sounds and probably is incredibly simple, I'm having syntax issues.  I've tried following and using syntax from other threads and haven't had much luck.

:athlete has_many :athlete_pictures and athlete_pictures belongs_to :athlete

Controller:

    @athletes = Athlete.find(:all)
    @athlete_pictures = @athletes.athlete_pictures.thumbnail("thumb").paginate_by_id @athlete, :per_page => 10, :page => params[:page],
                                                                                     :order => @athlete.last_name

View:
<% @athletes.each do |a| %>
<div>
    <%= image_tag a.athlete_picture.public_filename -%>
    <%= h a.name %>
    <%= link_to 'View Profile', :action => 'view_profile', :id => a %>
</div>
<% end %>



<%= will_paginate @athletes %>


And I get the error:

undefined method `athlete_pictures' for #<Array:0xb6b35cfc>

I have a feeling I need to put something in the model to handle this, but am not sure if I am on the right track at all after trying so many different things smile

Thanks in advance for looking!

Last edited by skerf (2007-10-07 01:33:12)

Re: Having trouble figuring out will_paginate and attachment_fu model

Evidently I'm also having trouble "figureing" out how to spell. hmm

Re: Having trouble figuring out will_paginate and attachment_fu model

problem is the second line in your controller, in more than one way ..
in the first you do a find for all athletes, so the resulting @atheletes is an array containing the reconrds.
then in the second line you call the association on that array, which of course won't work.

Also, you call paginate on a method of the association (which in itself is just wrong syntactically), which makes it look as if you want to paginate the pictures, while in the view, you paginate the athletes. i guess what you are looking for is something like this:

#Controller:
@athletes = Athlete.paginate(:per_page => 10, :page => params[:page], :order => @athlete.last_name, :include => :athlete_pictures)

#View:
<% @athletes.each do |a| %>
  <div>
  <% a.athlete_pictures.each do |pic| %>
    <%= image_tag pic.public_filename(:thumb) %>
  <% end %>
  <%= h a.name %>
  <%= link_to 'View Profile', :action => 'view_profile', :id => a %>
  </div>
<% end %>

<%= will_paginate @athletes %>


This will paginate the athletes and eager-load the associated pictures in the controller.
In the view, you loop through the athletes, and for each artists, you loop through the athlete's pictures. But you say you only want to show one image, "his thumb" ... is this a special image in the has_many association? cos every picture can have a thumb.

Last edited by Duplex (2007-09-29 11:39:23)

Re: Having trouble figuring out will_paginate and attachment_fu model

Duplex:

Thanks so much for the reply!

Yes, there will ultimately be many thumbnails, but for now I am only allowing one so I can gain understanding wink  Once I understand better, I will allow multiple thumbnails/images.

I'm going to give this code a try after I eat some lunch!

Thank you again!

Re: Having trouble figuring out will_paginate and attachment_fu model

OK, now I am getting this NoMethodError error message:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.last_name

I actually tried something earlier that was very nearly identical to what you have in the controller before and got the same problem.

I also tried something similar to this thread:

http://railsforum.com/viewtopic.php?id=9578

which got me where I ended up.

Any suggestions?

Re: Having trouble figuring out will_paginate and attachment_fu model

Oh, just noticed, the :order option should  :order => "athletes.last_name".
You have to pass it an SQL fragment (tablename.columnname), not some ruby code. plus, as the @athlete variable doesn't exist obviously, it throws this error. I should have seen this in the first place ...

Try:

@athletes = Athlete.paginate(:per_page => 10, :page => params[:page], :order => "athletes.last_name", :include => :athlete_pictures)
#

Last edited by Duplex (2007-09-29 14:10:05)

Re: Having trouble figuring out will_paginate and attachment_fu model

Duplex:

Thank you very much - it's now paginating as long as no images are present.

Now I am getting a NoMethodError on the picture:

undefined method `public_filename' for #<AthletePicture:0xb6db0c84>

Is there anything readily apparent in the code that would cause this?  If not, I have a hunch about my table.

Thanks again!

Last edited by skerf (2007-09-29 14:38:00)

Re: Having trouble figuring out will_paginate and attachment_fu model

Well, as usual, the remaining quirks were simple things I missed.

Thanks loads for your help, Duplex.

Re: Having trouble figuring out will_paginate and attachment_fu model

I can't for the life of me figure two things out.  I've implemented the above code.  I get pagination with my athletes and images, when there is one image.

<% @athletes.each do |a| %>
  <div>
  <% a.athlete_pictures.each do |pic| %>
    <%= image_tag(pic.public_filename(:thumb)) %>
  <% end %>
  <%= h a.name %>
  <%= link_to 'View Profile', :action => 'show', :id => a.id %>
  </div>
<% end %>

yields the following HTML:

    <img alt="Logo_thumb" src="/athlete_pictures/0000/0023/logo_thumb.jpg?1191270546" />
 
    <img alt="Logo_thumb_thumb" src="/athlete_pictures/0000/0023/logo_thumb_thumb.jpg" />

And there is no such file as logo_thumb_thumb.jpg, only logo_thumb.jpg.

My second problem is doing as Duplex suggested and attempting to work when there are multiple thumbnails created.  I'd like to be able to choose a specific one of the thumbs that were generated on save, but am not sure how hmm

Thanks again for looking!

Re: Having trouble figuring out will_paginate and attachment_fu model

Aha!  I've just figured out how I can specify which thumbnail I want to use by digging into the README a little.  I didn't realize :thumbnails accepted a hash.  Nice.

I still can't figure out why I'm getting multiple image tags in my HTML, though.