回形针设置噩梦。建议?

时间:2015-02-17 02:17:16

标签: paperclip

我们在不同的表格和模型中有项目和照片。这是我们的架构:

 create_table "projects", force: :cascade do |t|
    t.string   "title"
    t.integer  "price"
    t.datetime "created_at",                      null: false
    t.datetime "updated_at",                      null: false
    t.string   "description"
    t.boolean  "retired",         default: false
    t.integer  "tenant_id"
    t.date     "requested_by"
    t.integer  "repayment_rate"
    t.date     "repayment_begin"
  end



 create_table "photos", force: :cascade do |t|
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.integer  "project_id"
  end

在项目模型中我有:

has_attached_file :image
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]

这是否可以作为设置回形针的方式?或者项目和照片必须全部放在一张桌子上?

1 个答案:

答案 0 :(得分:0)

是的,这是可以接受的,但您的has_attached_filevalidates_attachment_content_type必须是照片模型,而不是项目模型。

# project.rb
class Project < ActiveRecord::Base
  has_many :photos
end

# photo.rb
class Photo < ActiveRecord::Base
  belongs_to :project
  has_attached_file :image #, styles: { svga: ["800x600!", :jpg], big: ["500x375!", :jpg], small: ["190x143!", :jpg], thumb: ["95x71!", :jpg] }
  validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"] # or do_not_validate_attachment_file_type :image
end