【问题标题】:rails 3 polymorphic association with paperclip and multiple modelsrails 3 与回形针和多个模型的多态关联
【发布时间】:2012-05-23 07:40:48
【问题描述】:

我想用回形针进行多态关联,并允许我的用户拥有一个头像和多个图像。

附件型号:

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

用户模型:

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

用户控制器:

def edit
   @user.build_avatar
end

用户查看表单:

<%= 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 %>

当我尝试保存更改时出现错误 => 未知属性:头像

如果我在 has_one 关联中删除 :class_name => 'attachment' 我会收到错误 => 未初始化的常量 User::Avatar

我还需要将头像附加到博客帖子,所以我需要关联是多态的(或者至少我认为是这样)

我很困惑,任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 paperclip polymorphic-associations


    【解决方案1】:

    我确实有一个项目正在成功使用回形针和多态关联。让我告诉你我有什么,也许你可以将它应用到你的项目中:

    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
    

    歌曲表格和专辑表格包含以下内容:

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

    不要忘记在表单中包含 :html => { :multipart => true }

    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
    

    最后,摘自 song_controller.rb:

    def new
        @song = Song.new
        @song.build_artwork
    end
    

    【讨论】:

    • @kaigth,对不起,我给了你这样的麻烦。我应该从这个开始。最良好的祝愿。
    • Artwork 模型中的附件样式是否可以根据多态关系对专辑和歌曲有所不同?
    • @RamkumarMK 我相信你们的模型都会共享相同的艺术风格。因此,在那个位置,您应该定义任何模型所需的所有样式,或者重新考虑是否需要使用多态关联而不是每种情况独有的东西
    • @Brett 在我的情况下,不同模型的艺术风格必须不同。 this文章推荐sti和多态组合方案,解决了我的问题。
    【解决方案2】:

    我不确定你真的需要多态。这种使用 has_many :through 的方法怎么样?通俗的说,用户有一个头像,里面有多张图片,通过这个关联可以调用 User.images 来获取与头像关联的图片集合。

    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
    

    说了这么多,我想知道为什么你还需要经历这一切。为什么不干呢

    class User < ActiveRecord::Base
      has_many :avatars
    end
    

    这将为您提供任意数量的图像(头像)。

    【讨论】:

    • 太棒了,但我也想在博客文章中附加头像,所以我仍然需要使用多态关联 Post::Avatar
    • @kaigth 你不想将用户附加到博客帖子,然后获取他们关联的头像吗?
    • @Brett 我希望人们能够为博客帖子分配单独的头像,这样他们的帖子就可以是唯一的,而不是用户头像
    • 另外,我稍后会将图像添加到其他模型中,这就是为什么我需要它是多态的。
    猜你喜欢
    • 2018-01-05
    • 2011-07-06
    • 1970-01-01
    • 2011-01-30
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多