0
我有一个厕所和照片模型和照片有一个多态关联。 我有一个从csv文件收集信息的rake任务。我想更新图片,但因为它不是厕所桌子的一部分,所以我很难解决这个问题。我用回形针处理我的图像。如何更新rake任务中的多态图像?
我的任务是这样的
namespace :import do
desc "Importing toilets from csv"
task toilets: :environment do
filename = File.join Rails.root, "toilets.csv"
counter = 0
CSV.foreach(filename, headers: true) do |row|
toilet = Toilet.create(
name: row['name'],
latitude: row['latitude'],
longitude: row['longitude'],
address: row['address'],
image: row['image']
)
puts "#{toilet} - #{toilet.errors.full_messages.join(",")}" if toilet.errors.any?
counter += 1 if toilet.persisted?
end
puts "Imported #{counter} toilets"
end
end
空调型号
has_many :photos, :as => :imageable
accepts_nested_attributes_for :photos
照片模式
belongs_to :toilet
belongs_to :imageable, :polymorphic => true
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/