Попытка получить значение от объекта не работает.Невозможно получить значение атрибута из модели 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 then profile.assets.first.file работает BUT profile = Profile.where (: user_id => 2), тогда profile.assets.first.file возвращает NoMethodError: undefined method 'assets ' для # <# <Класс: 0x007ff8ea845b80>: 0x007ff8eccd22f0>. И user_id 1 и 2 имеют действительный актив в столбце активов db, поэтому это путаница. – Rubytastic
Есть ли более одного профиля с 'user_id = 2'? 'Profile.where (: user_id => 2) .count> 1'? – bricker
bricker: no, который возвращает false, данные в db кажутся прекрасными, поэтому я не могу понять это – Rubytastic