6

我想与回形针做多态关联,并允许我的用户有一个化身和多个图像。rails 3与回形针和多个模型的多态关联

附件模型:

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>“附着” ::头像

我还需要附加的化身,以博客,所以我需要联想到是多态的(或至少我这么认为)

我难倒任何帮助,将不胜感激。

回答

6

我确实有一个成功使用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 %> 

唐“T忘记包括:HTML => {:多=>真}与表单

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 
+0

啊,就是我所需要的,谢谢Brett! – kaigth

+0

@kaigth,对不起,我给了你这样一个绕圈。我应该刚刚开始。最好的祝愿。 – Brett

+0

基于多态关系,艺术品模型中附件的样式可以不同于专辑和歌曲吗? – ramkumar

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 

这会给你尽可能多的图像(化身),你想要的。

+0

多数民众赞成真棒,但我也想化身连接到博客,所以我仍然需要使用多态关联后::头像 – kaigth

+0

@kaigth你岂不要附加用户博客,和然后抓住他们相关的头像? – Brett

+0

@ Brett我希望人们能够为博客文章分配独立的头像,所以他们的帖子可以是唯一的,而不是用户头像 – kaigth