Topic: simple question about models and loading child records from a list

I have two models that look like this:

class Mail < ActiveRecord::Base
  has_many :pictures
end

and

class Picture < ActiveRecord::Base
  belongs_to :mail
end

when I want to show a single mail with it's pictures, I do this:

  def show
    @mail = Mail.find(params[:id])
    @mail.pictures
  end

but when I try to load into a list of mails, I can't get it to load its pictures like this:

 def list_my_emails
   user_id=session[:user_id]
   @mails=Mail.find(:all,
            :conditions=>"user_id=user_id")
   @mails.pictures
end

it seems that if the first way worked, that this second way should also work. Sorry for these simple questions.

Re: simple question about models and loading child records from a list

Your first example:

def show
    @mail = Mail.find(params[:id])
    @mail.pictures
end

Uses the find by id finder, which returns an individual item (scalar). BTW: I don't know how you're displaying this in your view, but I assume the @mail.pictures line belongs in the view.

The second example:

def list_my_emails
   user_id=session[:user_id]
   @mails=Mail.find(:all,
            :conditions=>"user_id=user_id")
   @mails.pictures
end

Uses the find(:all) finder, which always returns an array even if only one element is present. Therefore, @mails is always of type Array. If you use @mails[0].pictures you should see the pictures for the first mail.