Can't access polymorphic associated model's attributes

Hi!!

I have a few models that use a Photo polymorphic model. It's all good and fine for saving or accessing the photos' attributes via console or inspect or logger, but if I try to call them directly in the view with, for example, @banner.photo.file, it returns a undefined method 'file' etc for nil:Nil

Photos are being uploaded via paperclip and all models are set with has_one :photo, :as => :imageable and accepts_nested_attributes_for :photo, :allow_destroy => true. And the Photo model is set as belongs_to :imageable, :polymorphic => true and has_attached_file :file.

These are the columns for the Photo model:

id:integer file:string imageable_id:integer imageable_type:string file_file_name:string file_content_type:string file_file_size:integer (besides created_at etc)

In my view:

<% @banners.each do |b| %>   <%= b.photo.inspect %><br /> <% end %> You can see the inspect at http://bdyvu2.hospedagemweb.net/

Many thanks in advance!! Going out of my mind here!! (been at it for days!)

That means that @banner.photo is nil. Add code to your view to test @banner.photo before accessing @banner.photo.file.

Colin

oh hi! I did... I can see it if I inspect @banner.photo, but if I try to access it directly (using any of the name variations), it doesn't work - for ANY of the attributes :confused:

thankss, yara

Please quote the previous message when replying and insert your reply into the previous message so that we don't have to find the previous mail to know what you are talking about. Thanks.

  You say that @banner.photo works but accessing it directly does not, what do you mean by that? You could copy and paste the console output showing the inspect and explain what you mean by "doesn't work".

Colin