简体   繁体   中英

paperclip - styles lambda

I have a script from which I load my rails environment. (

When I build an attachment and save its parent everything saves and gets created by the attachment style is always ["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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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