0
votes

I have a polymorphic Attachment model to save images uploaded by users. Users can set any of their own as a profile pic.

To this, I created an "avatar_id" column in the User database to basically save the attachment id to create the reference. I also added an avatar action to the User model:

def avatar 
  self.attachments.first(:conditions => ['id = ?', self.avatar_id])
end

The problem is that if I try to make this work

<%= @user.avatar.url %>

It doesn't work because the url method doesn't exist. I need to specify that is an paperclip object but I don't get where and how I should do it. I'm probably missing something obviously here.

1

1 Answers

2
votes

Your Attachment model should have a set of attachment fields: maybe attachment_file_name, attachment_content_size, etc.

So when you reference @user.avatar, you're really just referencing the entire Attachment record rather than the paperclip-specific columns.

Try this, substituting "attachment" for whatever you called your paperclip columns:

<%= @user.avatar.attachment.url %>