Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails 3与回形针和多个模型的多态关联_Ruby On Rails_Ruby On Rails 3_Paperclip_Polymorphic Associations - Fatal编程技术网

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

Ruby on rails rails 3与回形针和多个模型的多态关联,ruby-on-rails,ruby-on-rails-3,paperclip,polymorphic-associations,Ruby On Rails,Ruby On Rails 3,Paperclip,Polymorphic Associations,我想与回形针进行多态关联,并允许我的用户拥有一个化身和多个图像 附件型号: class Attachment < ActiveRecord::Base belongs_to :attachable, :polymorphic => true end class Avatar < Attachment has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x18

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

附件型号:

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
用户控制器:

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 %>
{:multipart=>true}do | f |%>
当我试图保存更改时,我得到错误=>unknown属性:avatar

如果我删除has\u one关联中的:class\u name=>“attachment”,我会得到错误=> 未初始化常量User::Avatar

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


我被难住了,任何帮助都将不胜感激。

我不确定你是否真的需要多态性。这种方法如何,它使用了很多:通过?简单地说,用户有一个具有多个图像的化身,通过此关联,您可以调用user.images以获取与该化身关联的图像集合

class用户:化身
结束
类Avatar{:thumb=>“150x150>”,:view=>“260x180>”,
结束
说了这么多,我想知道你为什么要经历这一切。为什么不直接做呢

class User < ActiveRecord::Base
  has_many :avatars
end
class用户

这将为您提供您想要的任意多的图像(化身)。

我确实有一个正在进行的项目,该项目成功地使用了回形针和多态关联。让我向您展示我拥有的,也许您可以将其应用到您的项目中:

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

这太棒了,但我还想将头像附加到博客帖子上,所以我仍然需要使用多态关联帖子:Avatar@kaigth你不想将用户附加到博客帖子上,然后抓取他们相关的头像吗?@Brett我希望人们能够为博客帖子分配单独的头像,所以他们的帖子可以是独一无二的,而不是用户的头像,所以我以后会在其他模型中添加图像,这就是为什么我需要它是多态的。@kaigth,很抱歉我给了你这样的解释。我应该从这个开始。最好的祝愿。基于多态关系,专辑和歌曲的Artwork model中附件的样式是否可以不同?@RamkumarMK我相信您的模型都会共享相同的Artwork样式。因此,在这个位置,您应该定义任何模型需要的所有样式,或者重新考虑是否需要使用多态关联,而不是每个模型所特有的样式case@Brett在我的情况下,不同型号的艺术品风格必须不同。文章介绍了sti和多态组合解决方案,解决了我的问题。
class User < ActiveRecord::Base
  has_many :avatars
end
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 %>
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
def new
    @song = Song.new
    @song.build_artwork
end