Trying to get a value from a object isn’t working out.
class Asset < ActiveRecord::Base
attr_accessible :description,
:file,
:file_cache
belongs_to :attachable,
:polymorphic => true
mount_uploader :file, AssetUploader
In console
profile = Profile.first
Profile Load (0.5ms) SELECT `profiles`.* FROM `profiles` LIMIT 1 etc
profile.assets.inspect
Asset Load (0.6ms) SELECT `assets`.* FROM `assets` WHERE `assets`.`attachable_id` = 1 AND `assets`.`attachable_type` = 'Profile'
=> "[#<Asset id: 1, description: nil, file: \"fa731ee80a.jpg\", attachable_id: 1, attachable_type: \"Profile\", created_at: \"2012-01-30 00:29:21\", updated_at: \"2012-02-07 22:13:17\">]"
How would I get the file attribute from this?
Tried numerous things but I just seem to be unable to figure it out
profile = Profile.first then profile.assets.first.file works
BUT
profile = Profile.where(:user_id => 2 ) then profile.assets.first.file returns a NoMethodError: undefined method `assets' for #<#
Quick answer:
profile.assets.first.fileActual answer: Your
profilemodel should have ahas_one :asset, :as => :attachableif each profile will only have one attached file, but it seems that you have ahas_many.Update
Try:
Or: