Boss Nass
Boss Nass

Reputation: 3522

Ruby on Rails Relationship Troubles

I am creating a relationship database between 'property' and 'property_images' whenever I try to reference the property_images by the following call = image_tag property.property_images.image_url.to_s, :size => '240x180' I get the following error

undefined method `image_url' for #<ActiveRecord::Relation:0x007fe8d2e95300>

though if I do <h1><%= property_image.property.title %></h1> it returns the correct value

I have my 2 models like so

 class PropertyImage < ActiveRecord::Base
  belongs_to :property
  attr_accessible :feature, :image, :property_id
  mount_uploader :image, ImageUploader
end


    class Property < ActiveRecord::Base
  belongs_to :agency
  has_many :property_images

so the relationship should be working the way I want, though it doesnt appear to be case

Upvotes: 0

Views: 30

Answers (1)

kik
kik

Reputation: 7937

property.property_images is not a PropertyImage, it's a collection of property images.

You have to tell which one you want to use, for example the first :

= image_tag property.property_images.first.image_url.to_s, :size => '240x180'

Upvotes: 2

Related Questions