嘗試從對象獲取值無法解決問題。無法從ActiveRecord模型中獲取屬性值
class Asset < ActiveRecord::Base
attr_accessible :description,
:file,
:file_cache
belongs_to :attachable,
:polymorphic => true
mount_uploader :file, AssetUploader
在控制檯
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\">]"
我將如何得到這個文件的屬性? 試過無數的事情,但我只是似乎無法弄清楚
profile = Profile.first then profile.assets.first.file works
但
profile = Profile.where(:user_id => 2) then profile.assets.first.file returns a NoMethodError: undefined method `assets' for #<#
profile = Profile.first然後profile.assets.first.file工程BUT profile = Profile.where(:user_id => 2)then profile.assets.first.file返回一個NoMethodError:undefined方法'assets'爲#<#:0x007ff8eccd22f0>。 user_id 1和2都在db的assets列中有有效的資產,所以這個混淆了。 –
Rubytastic
2012-02-10 09:13:58
「user_id = 2」是否有多個配置文件? 'Profile.where(:user_id => 2).count> 1'? – bricker 2012-02-10 09:20:27
bricker:不,返回false,db中的數據看起來很好,這就是爲什麼我無法掌握這個 – Rubytastic 2012-02-10 09:21:40