回形针 - 样式lambda

时间:2012-06-20 19:17:36

标签: ruby-on-rails paperclip

我有一个脚本,我可以从中加载我的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

我猜我在这里忽略了一些愚蠢的东西,但我会感激任何指示:)

1 个答案:

答案 0 :(得分:3)

你在lambda上的语法看起来有点时髦。我不确定那个三元运算符是不是在咆哮“意外”:'“。

试一试:

Attachment < AR::Base
  has_attached_file :doc, styles: lambda {|attachment| { thumb: (attachment.instance.category_id == 2 ? ["500>", 'jpg'] : ['100>', 'jpg'])}}
  belongs_to :attachable, polymorphic: true
end