0

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

我猜我在这里忽略了一些愚蠢的事情,但我会很感激任何指针:)

4

1 回答 1

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
于 2012-06-20T19:28:47.530 回答