Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3425714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:37:37+00:00 2026-05-18T06:37:37+00:00

This is a pretty frustrating newb question, but I can’t seem to get iteration

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:37:38+00:00Added an answer on May 18, 2026 at 6:37 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is pretty frustrating. I'm trying to get the size of an AVURLasset, but
This is pretty much a duplicate question but instead of using Castle Dynamic Proxy
This is a pretty stale question but frankly I'm yet to find it answered
This has to be a pretty basic question but I cannot for the life
This seems pretty trivial, but it is now frustrating me. I am using C#
Seems I've outdone myself. All the while I was creating this pretty little 'latest
This is pretty straight forward. EDIT: Updated question and added fourth echo. Here is
This is pretty interesting, this site runs a speed test and compares all of
EDIT The original reasoning for this question was a pretty serious case of bad
this is pretty frustrating... basically i'm creating a menubar on a site, and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.