尝试从对象获取值无法解决问题。无法从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