views:

29

answers:

0

Assumptions:

  1. A contact has many notes.
  2. A contact has the ID of 1.
  3. A note belongs to a user.
  4. A user has the field name, "name".

Using the prawn gem and prawnto plugin, I've noticed the following:

In my view (for the PDF):

for note in @contact.notes
  pdf.text "#{note.user.name}"
end

This is ideally how I'd like to display the PDF, but I'm noticing something odd. When I derive the notes in the block using an association, like @contact.notes, the above embedded rails code inside of pdf.text will not populate. I get a nil error when it attempts to get the name of the user. (#{note.user.name})

However, if you do the following:

for note in @notes
  pdf.text "#{note.user.name}"
end

And in your controller use the following code to populate the @notes variable:

@notes = Note.find_all_by_contact_id(1)

The above mentioned block works fine. The association of note.user.name populates the PDF view perfectly.

My question: Why would the association work when using a hard-coded database call from the controller vs. using an association like @contact.notes?

In the console, the result of each one is identical (an array with notes).