Ruby on rails 回形针-lambda风格
我有一个脚本,从中加载rails环境。( 构建附件并保存其父级时,附件样式保存和创建的所有内容始终为[“100>”,“jpg”] 我的剧本:Ruby on rails 回形针-lambda风格,ruby-on-rails,paperclip,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 :attachm
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
House:可附加
结束
附件,'jpg']:['100>,'jpg']}
has_attached_file:doc,styles:lambda{| attachment |{thumb:(attachment.instance.category_id==2?[“500>”,'jpg']:['100>,'jpg'])}#此时category_id始终为零,但仍保存在数据库中
属于:可附加,多态:true
结束
我猜我在这里忽略了一些愚蠢的事情,但我希望有任何提示:)你在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
附件,'jpg']:['100>,'jpg'])}
属于:可附加,多态:true
结束
你在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
附件,'jpg']:['100>,'jpg'])}
属于:可附加,多态:true
结束
这不是一项复制粘贴工作,我只是在离开工作电脑的时候快速模拟了一下。我编辑了op。这不是一个复制粘贴的工作,我只是在离开工作电脑的时候快速地模拟了它。我编辑了作品。