rails 3与回形针和多个模型的多态关联

时间:2012-05-15 18:33:01

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

我想与paperclip建立多态关联,并允许我的用户拥有一个头像和多个图像。

附件型号:

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

当我尝试保存更改时,我得到错误=&gt;未知属性:头像

如果我删除:class_name =&gt; has_one关联中的'attachment'我得到错误=&gt; 未初始化的常数User :: Avatar

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

我很难过,任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:7)

我的作品中有一个成功使用Paperclip和多态关联的项目。让我告诉你我拥有的东西,也许你可以将它应用到你的项目中:

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 =&gt; {:multipart =&gt;使用表格

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

最后,摘自songs_controller.rb:

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

答案 1 :(得分:0)

我不确定你真的需要多态。这种方法怎么样,使用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

可以根据需要为您提供尽可能多的图像(头像)。