Question

This is a pretty frustrating newb question, but I can't seem to get iteration of objects to work at all in my rails project. I have two models, "Photo" and "LandUse" which both have a has_and_belongs_to_many association. Everything works as expected in the console.

However, in the photo show action I cannot seem to decipher what code would produce a list of the land uses associated with the photo. My code looks like this:

<% for use in @photo.land_uses %>
  <%= use.name %>
<% end %>

That doesn't produce any output. I also tried:

<% @photo.land_uses.each do |use| %>
  <%= use.name %>
<% end %>

... and that also doesn't work.

In the console if I try something like this:

photo = Photo.first
photo.land_uses

I get a list of all the land uses that the photo belongs to, no surprises.

So how do I iterate through that list of uses and print the name of each use?

I'm new to Ruby, but if I were trying to do the same thing in PHP is would be a simple foreach loop:

foreach($photo->land_uses as $use) {
  echo $use->name;
}

So what am I missing here?


Update: here are the models as requested:

--Photo Model--

class Photo < ActiveRecord::Base
    belongs_to :user
    belongs_to :transect
    belongs_to :focus
    has_and_belongs_to_many :land_uses

    validates :caption, :presence => true
    validates :place, :presence => true
    validates :city, :presence => true
    validates :country, :presence => true



    has_attached_file :file,
                                        :processors => [:watermark],
                                        :styles =>  { :standard => "631x631>",
                                                                    :marked => { :geometry => "631x631>", :watermark_path => "#{Rails.root}/public/images/watermark.png", :position => 'Center' },
                                                                    :thumbnail => "174x130#"
                                                                },
                                        :storage => :s3,
                                        :s3_credentials => "#{Rails.root}/config/s3.yml",
                                        :path => ":user_id/:year/:month/:id/:style.:extension"

    geocoded_by :location, :latitude  => :lat, :longitude => :lng

    after_validation :fetch_coordinates

    def location
        [place,city,state,country].delete_if{|val| val==''||nil}.join(', ')
    end
end

-- LandUse Model --

class LandUse < ActiveRecord::Base
    has_and_belongs_to_many :photos

    validates_uniqueness_of :name
end

RE: Error Messages... I don't get an error message with either the for or each approach, I just get a blank space where there should be a list of land uses.

Thanks!

Was it helpful?

Solution

How embarrassing...

I found the problem. Both of my original solutions for printing the name of the land use worked just fine. The problem was I didn't seed the data correctly, so the photos I was testing did not have any land use saved (hence no error message but no content either). Fixed the seed file and the view renders as expected.

Thanks for taking a look.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top