我有一个脚本,我可以从中加载我的 rails 环境。(
当我构建附件并保存其父级时,所有保存并由附件样式创建的内容始终为 ["100>", "jpg"]
我的脚本:
require './config/environment.rb'
house = House.find(1)
house.attachments.build(doc: File.new('myfile.pdf'), category_id: 2)
house.save
楷模
House < AR::Base
has_many :attachments, :as => :attachable
end
Attachment < AR::Base
### has_attached_file :doc, styles: lambda {|attachment| {thumb: (attachment.instance.category_id == 2 ? ["500>", 'jpg'] : ['100>', 'jpg']} )}
has_attached_file :doc, styles: lambda {|attachment| {thumb: (attachment.instance.category_id == 2 ? ["500>", 'jpg'] : ['100>', 'jpg'] )}} #category_id is always nil at this point but still still saves in the database
belongs_to :attachable, polymorphic: true
end
我猜我在这里忽略了一些愚蠢的事情,但我会很感激任何指针:)