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 6538161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:42:13+00:00 2026-05-25T10:42:13+00:00

I try to understand the behavior of associations, but I am doing something wrong.

  • 0

I try to understand the behavior of associations, but I am doing something wrong.

I have these two models:

class Album < ActiveRecord::Base
  has_many :photos
end

class Photo < ActiveRecord::Base
  belongs_to :album
end

This means that one album could have many photos and every photo belongs to one album.

Now, if I have this code in the view (assuming I have set @album = 106 in the controller, which is the ID number of the album):

@album.photos.each_with_index do |pic, index|
  ...
end

And I got the error: ActionView::Template::Error (undefined method 'photos' for 106:Fixnum):

Why do I get this error message? I thought I have to set the @album variable ID of album and then the loop will be searching the photos in the table Photos by album_id (album_id=106).

What I am doing still wrong?

  • 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-25T10:42:14+00:00Added an answer on May 25, 2026 at 10:42 am

    You have to get the model instance to fetch the associations, like this:

    @album = Album.find(106)
    

    To sort the photos by creation date, do it like this:

    @album.photos.order('created_at DESC').each_with_index do |pic, index|
      ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can fix the below exception with a try-catch loop but I cannot understand
Here is a weird behavior that I don't understand about custom views. I have
I try to understand Firefox's behavior regarding the added prevent this page from creating
I have some behavior that I don't understand. I'm using RavenDB, and I'm using
I try to implement the abstract DbCommand class (like OdbcCommand, OleDbCommand, ...) but a
I try to learn Rspec , but I don't understand what is that. Let
I try to understand the memory management in ObjectiveC and still some things are
I used mmap(just try to understand how mmap works) to allocate 96k anonymous memory,
I'm new to win32 API programming and I try to understand source code of
I try to compare Mnesia with more traditional databases. As I understand it tables

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.