rails 3 polymorphic association with paperclip and multiple models

后端 未结 2 1485
日久生厌
日久生厌 2020-12-30 12:27

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

Attachment model:

         


        
相关标签:
2条回答
  • 2020-12-30 12:56

    I'm not sure you really need to be polymorphic. How about this approach, which uses has_many :through? In plain English, the user has one avatar which has multiple images, and through this association you can call User.images to get the 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 of this, I am left to wonder why you need to go through all this anyway. Why not just do

    class User < ActiveRecord::Base
      has_many :avatars
    end
    

    which would give you as many images (avatars) as you want.

    0 讨论(0)
  • 2020-12-30 13:04

    I do have a project in the works that is successfully using 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
    

    the songs form and the albums form include this as a 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 songs_controller.rb:

    def new
        @song = Song.new
        @song.build_artwork
    end
    
    0 讨论(0)
提交回复
热议问题