Starter question about paperclip / relations in rails 3

Hello,

i’m trying out Rails 3 and i’ve created a little app with paperclip.
My problem is that i cant get the assets for a user in the index
action but in the edit action it works. i have the flowing set up

class User
has_many :assets

class Asset
belongs_to :user
has_attached_file :asset,
:styles => { :medium => “300x300>”,
:thumb => “100x100>” }


In my edit action i can get the assets for the user easy by doing:

<% f.fields_for :assets do |asset_fields| %>

<% unless asset_fields.object.new_record? %>

  <tr>
    <td><%= link_to 

image_tag(asset_fields.object.asset.url(:thumb)),
asset_fields.object.asset.url(:original) %>

<%= f.label :destroy, ‘Verwijder’ %>
<%= asset_fields.check_box(:_destroy) %>

<% end %>
<% end %>

Now in my index action i want to display the user.assets also but i
get the error undefined method `url’ for xx

in the user controler i get the users by
@users = User.all(:include => :assets)
#@users = User.all()

in the view i have
<% @users.each do |user| %>


<%= user.firstname %><%= user.lastname %>
<%= debug(user) %>
<% unless current_user.nil? %>
<% if current_user.id == user.id %>
<%= link_to “Edit”, edit_user_path(user) %>
<% end %>
<% end %>
<%= debug(user.assets) %>
<% user.assets.each do |assetfield| %>
<%= assetfield.url(:thumb) %>
<% end %>
  </div>
<% end %>

and i get the error undefined method `url’ for #<Asset:
0x000001058cce08> so the asset object is not there i gues. I’m having
trouble to understand whats wrong because the realtion is fine. I hop
somebody here can help me understand this and help me out?

thanks in advance!

Hey

When you do (user.assets.each do |assetfield|) you are getting each
Asset
object associated to user in the assetfield variable, but still you need
to
access the attachment property which is ‘asset’. So basically you just
need
to change assetfield.url(:thumb) for assetfield.asset.url(:thumb) and it
should work fine. Check that this is done in the edit form
as asset_fields.object.asset.url(:thumb) (where asset_fields.object
represents the same Asset object as assetfield)

Hope this helps. Good luck!

Alejandro,

Thank you for taking the time to answer my question and you where
right. just by refering the asset object by
assetfield.asset.url(:thumb) it worked perfect.