【问题标题】:user has many images using paperclip in rails 3用户在 rails 3 中有许多使用回形针的图像
【发布时间】:2013-12-12 08:22:41
【问题描述】:

我一直在使用paperclip,模型为user has_many :imagesimage belongs_to:user。现在我想让多态性为图像模型工作,这样用户,post 有很多图像和@ 987654324@.我正在努力,但我不自信,因为我不清楚回形针的概念。现在这些是我所做的更改,但我需要做哪些进一步的更改才能获得user has many images 的关联和images belongs to imageable ,polymorphic =>true.Below 是我所做的,但没有得到任何结果。任何帮助将不胜感激......。我只需要图像的多态性,例如用户有很多图像,帖子有很多图像.....等其中图像属于可成像,:多态=真。

image.rb

belongs_to :imageable,:polymorphic=>true
has_attached_file :avatar, :url  => "/assets/images/#{imageable_type}/#{imageable_id}/:style/:basename.:extension",
             :path => ":rails_root/public/assets/images/#{imageable_type}/#{imageable_id}/:style/:basename.:extension", :default_url => 'dashboard/default_avatar.jp

g'

user.rb

has many:images ,:as=>imageable,:dependent=>destroy

将文件迁移到图像

class CreateImages < ActiveRecord::Migration
  def change
    create_table :images do |t|
      t.string :name
      t.references :site
      t.string :image_file_name, :null => true
      t.string :image_content_type, :null => true
      t.integer :image_file_size, :null => true
      t.timestamps
    end
  end
end

为 user.rb 迁移

class CreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      t.string :first_name
      t.string :last_name
      t.string :username
      t.string :mobile_number
      t.string :email
      t.references :image
      t.timestamps
    end

我将允许用户上传图片或编辑图片的视图表单

profile.html.erb.....使用remotipart gem

<%= form_for(@user,:url=>'add_image_to_user_path',:html => {:multipart => true},:remote=>true) do |f |%>
<%= f.file_field :images, :multiple => true %>
<%= f.submit "Add image" %>
<%end%>

users_controller.rb

    ##now how i should update the users image who has already logged in
    ##i tried @user.update_attribute(params[:user]) but i cannot see any update query in the log
 -   ##i can only see --->paperclip saving attachment
    def add_image_to_user
       @user=User.new
    end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 polymorphism paperclip


    【解决方案1】:

    听起来你需要has_many :through association。这将允许您将 imageuserpost 模型全部分开

    我认为您的问题是您将图像与特定对象的关联限制为您希望分配给 postuser 的特定对象


    Has_Many through

    在我们公司,我们只制作images belong_to :user(用于授权目的)。对于其他一切,我们使用带有 has_many :through 关联的连接模型,因为这允许您创建任意数量的关联

    我们是这样做的:

    #app/models/image.rb
    Class Image < ActiveRecord::Base
        has_many :image_posts, :class_name => 'ImagePost'
        has_many :posts, :class_name => 'Post', :through => :image_posts
    
        has_many :user_images, :class_name => 'UserImage'
        has_many :users, :class_name => 'User', :through => :user_images
    end
    
    #app/models/user.rb
    Class User < ActiveRecord::Base
        has_many :user_images, :class_name => 'UserImage'
        has_many :images, :class_name => 'Image', :through => :user_images
    end
    
    #app/models/post.rb
    Class Post < ActiveRecord::Base
        has_many :image_posts, :class_name => 'ImagePost'
        has_many :images, :class_name => 'Image', :through => :image_posts
    end
    
    #app/models/ImagePost.rb
    Class ImagePost < ActiveRecord::Base
        belongs_to :image
        belongs_to :post
    end
    
       #app/models/UserImage.rb
       Class UserImage < ActiveRecord::Base
            belongs_to :image
            belongs_to :user
       end
    

    连接模型在数据库中将如下所示:

    user_images
    id | user_id | image_id | extra | attributes | created_at | updated_at
    
    image_posts
    id | post_id | image_id | extra | attributes | created_at | updated_at
    

    这意味着您将使用accepts_nested_attributes_foruserpost 分配image_id。因为连接模型正在处理关联,所以图像保持不变。如果您认为适用,我可以发布如何执行此操作

    【讨论】:

      【解决方案2】:

      您的问题与回形针无关,完全与多态关联有关。现在,多态关联与常规关联非常相似 - 从 DB 模式的角度来看,唯一的区别是“归属”模型中存在另一列(在您的情况下,它是 imageable_type),它包含类相关记录。因此,首先,您应该从create_users 迁移中删除t.references :image,因为那里没有必要。其次,您应该将外键列和关联类型列添加到 create_images 迁移。您可以使用单个“指令”来执行此操作,如下所示:

      t.references :imageable, polymorphic: true
      

      这应该足够了。

      【讨论】:

        猜你喜欢
        • 2012-07-03
        • 2014-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 2014-11-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多