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?
You have to get the model instance to fetch the associations, like this:
To sort the photos by creation date, do it like this: