Rails 3 polymorphic association with a paper clip and several models

I want to make polymorphic associations with paperclip and allow my user to have one avatar and several images.

Attachment :

class Attachment < ActiveRecord::Base belongs_to :attachable, :polymorphic => true end class Avatar < Attachment has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" }, end class Image < Attachment has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" }, end 

User Model:

 has_one :avatar, :as => :attachable, :class_name => 'Attachment', :conditions => {:type => 'avatar'} accepts_nested_attributes_for :avatar 

User controller:

 def edit @user.build_avatar end 

User view form:

 <%= form_for @user, :html => { :multipart => true } do |f| %> <%= f.fields_for :avatar do |asset| %> <% if asset.object.new_record? %> <%= asset.file_field :image %> <% end %> <% end %> 

when I try to save changes, I get an error => unknown attribute: avatar

If I remove: class_name => 'attachment' in the has_one association, I get an error => uninitialized constant User :: Avatar

I also need to attach avatars to blog posts, so I need the association to be polymorphic (or at least think so)

I am at a dead end and any help would be greatly appreciated.

+6
source share
2 answers

I have a project in the works that successfully uses Paperclip and polymorphic associations. Let me show you what I have, and maybe you can apply it to your project:

 class Song < ActiveRecord::Base ... has_one :artwork, :as => :artable, :dependent => :destroy accepts_nested_attributes_for :artwork ... end class Album < ActiveRecord::Base ... has_one :artwork, :as => :artable, :dependent => :destroy accepts_nested_attributes_for :artwork ... end class Artwork < ActiveRecord::Base belongs_to :artable, :polymorphic => true attr_accessible :artwork_content_type, :artwork_file_name, :artwork_file_size, :artwork # Paperclip has_attached_file :artwork, :styles => { :small => "100", :full => "400" } validates_attachment_content_type :artwork, :content_type => 'image/jpeg' end 

song form and album formats include this as partial:

 <div class="field"> <%= f.fields_for :artwork do |artwork_fields| %> <%= artwork_fields.label :artwork %><br /> <%= artwork_fields.file_field :artwork %> <% end %> 

don't forget to include: html => {: multipart => true} with the form

artworks_controller.rb

 class ArtworksController < ApplicationController def create @artwork = Artwork.new(params[:artwork]) if @artwork.save redirect_to @artwork.artable, notice: 'Artwork was successfully created.' else redirect_to @artwork.artable, notice: 'An error ocurred.' end end end 

and finally an excerpt from song_controller.rb:

 def new @song = Song.new @song.build_artwork end 
+6
source

I'm not sure you really need to be polymorphic. What about this approach that has_many uses: through? In plain English, the user has one avatar that has several images, and through this link you can call User.images to get a collection of images associated with the avatar.

http://guides.rubyonrails.org/association_basics.html

 class User < ActiveRecord::Base has_one :avatar has_many :images, :through => :avatar end class Avatar < ActiveRecord::Base belongs_to :user has_many :images end class Image < ActiveRecord::Base belongs_to :avatar has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" }, end 

Having said all this, it remains for me to wonder why you still need to go through all this. Why not just do

 class User < ActiveRecord::Base has_many :avatars end 

which will provide you with as many images (avatars) as possible.

0
source

Source: https://habr.com/ru/post/915830/


All Articles