2012-10-04 22 views
0

我有一個Inventory.find(id),它會返回我需要的所有數據,包括使用回形針上傳 的圖標。然而,返回結果是這樣的後format.json回形針 - 在活動記錄查詢中獲取attachment.url

"icon_content_type": "image/jpeg", 
    "icon_file_name": "images_(2).jpeg", 
    "icon_file_size": 994, 
    "icon_updated_at": "2012-09-21T05:00:22Z", 

我怎麼包括「icon.url」,所以它會說像

 "icon_content_type": "image/jpeg", 
    "icon_file_name": "images_(2).jpeg", 
    "icon_file_size": 994, 
    "icon_updated_at": "2012-09-21T05:00:22Z", 
    "icon_url":"http://s3.amazonaws.com/*********/icons/000/000/130/original/images_(2).jpeg?1348203622" 

這裏是我的模型

has_attached_file :icon, :presence => false, :default_url => "/assets/logo.png" 
    validates_attachment :icon, 
    :content_type => { :content_type => ["image/jpg","image/png","image/jpeg"] }, 
    :size => { :in => 0..50.kilobytes } 

回答

0

See doc,你可以添加任何你需要的JSON:

your_object.to_json(:methods => :icon_url) 

對於更復雜的情況,您寧願使用json構建器。