2012-06-20 39 views
0

我有一個腳本,我從中加載我的rails環境。 (回形針 - 風格lambda

當我建立一個連接,並保存其母公司一切保存和獲取由依戀風格創建總是[「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 

我猜我忽略了一些愚蠢的在這裏,但我希望任何指針:)

回答

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 
+0

,這不是一個複製和粘貼的工作,我只是嘲笑它快,而我是從我的工作電腦了。我編輯了op。 –