Note that there are some explanatory texts on larger screens.

plurals
  1. POrails 3 polymorphic association with paperclip and multiple models
    primarykey
    data
    text
    <p>I want to make polymorphic associations with paperclip, and allow my user to have one avatar and multiple images.</p> <p><strong>Attachment</strong> model:</p> <pre><code>class Attachment &lt; ActiveRecord::Base belongs_to :attachable, :polymorphic =&gt; true end class Avatar &lt; Attachment has_attached_file :image, :styles =&gt; { :thumb =&gt; "150x150&gt;", :view =&gt; "260x180&gt;" }, end class Image &lt; Attachment has_attached_file :image, :styles =&gt; { :thumb =&gt; "150x150&gt;", :view =&gt; "260x180&gt;" }, end </code></pre> <p>User Model:</p> <pre><code>has_one :avatar, :as =&gt; :attachable, :class_name =&gt; 'Attachment', :conditions =&gt; {:type =&gt; 'avatar'} accepts_nested_attributes_for :avatar </code></pre> <p>User Controller:</p> <pre><code>def edit @user.build_avatar end </code></pre> <p>User View form:</p> <pre><code>&lt;%= form_for @user, :html =&gt; { :multipart =&gt; true } do |f| %&gt; &lt;%= f.fields_for :avatar do |asset| %&gt; &lt;% if asset.object.new_record? %&gt; &lt;%= asset.file_field :image %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>when I attempt to save the changes I get the error => unknown attribute: avatar</p> <p>if I remove the :class_name => 'attachment' in the has_one association I get the error => uninitialized constant User::Avatar</p> <p>I need to also attach avatars to blog posts, so I need the association to be polymorphic (or atleast i think so)</p> <p>I am stumped and any help would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload