简体   繁体   English

回形针-样式lambda

[英]paperclip - styles lambda

I have a script from which I load my rails environment. 我有一个脚本可以从中加载我的rails环境。 (

When I build an attachment and save its parent everything saves and gets created by the attachment style is always ["100>", "jpg"] 当我建立附件并保存其父对象时,附件样式保存和创建的所有内容始终为[“ 100>”,“ jpg”]

My Script: 我的剧本:

require './config/environment.rb'
house = House.find(1)
house.attachments.build(doc: File.new('myfile.pdf'), category_id: 2)
house.save

Models 楷模

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

I'm guessing I've overlooked something silly here but I would appreciate any pointers :) 我猜我在这里忽略了一些愚蠢的事情,但任何指针我都会感激的:)

Your syntax on that lambda looks a little funky. 您在该lambda上的语法看起来有些时髦。 I'm not sure how that ternary operator isn't barking an "unexpected ':'". 我不确定该三元运算符是否不会发出“意外的':'”。

Give this a shot: 试一下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM