Accessing the field of a model that belongs to another model using dot notation

Hey all,

with this below (a post has many blog_images and blog_image belongs to
post):

<% @posts.each do |post| %>

<div class="FeatureItem">
    <a href=""><img src="/images/<%=

post.blog_images.image_file_name %>" width=“220” height=“174” alt=""
style=“top: 27px; left: 13px;” />Click Here

    <div class="FeatureContent">
      <h2><a href=""><%= post.title %></a></h2>
      <p><%= post.body %></p>
      <p class="posted"><%= post.user_id %></p>

    </div>
</div>

<% end %>

I get undefined method image_file_name because image_file_name is a
field in blog_images table. So activerecord generates field names as
instance methods but above I’m trying to use it as a class method, so
since there is no class method image_file_name in BlogImage class, it
throws error. Is there a way that I can get image_file_name without
creating a class mehtod because I want to pull the data dynamically from
the table, where the image file belongs to blog post.

Thanks for response.

On Thu, Feb 24, 2011 at 11:47 AM, John M. [email protected]
wrote:

style=“top: 27px; left: 13px;” />Click Here

you problem here is that post.blog_images returns an array of
blog_images.
so the
error is thrown since you’re calling image_file_name on an array object.
try

post.blog_images.first.image_file_name

Phil

On Wed, Feb 23, 2011 at 9:47 PM, John M. [email protected]
wrote:

style=“top: 27px; left: 13px;” />Click Here
I get undefined method image_file_name because image_file_name is a
field in blog_images table. So activerecord generates field names as
instance methods but above I’m trying to use it as a class method, so
since there is no class method image_file_name in BlogImage class, it
throws error. Is there a way that I can get image_file_name without
creating a class mehtod because I want to pull the data dynamically from
the table, where the image file belongs to blog post.

Thanks for response.

You can’t call image_file_name on post.blog_images, because
post.blog_images
returns an array, and the array doesn’t have a method called
image_file_name. Check something like map; eg:
post.blog_images.map(&:image_file_name).

Thanks for replies.

This:

post.blog_images.first.image_file_name

outputs

undefined method `image_file_name’ for nil:NilClass

I think that means I called a method on a class that it cannot find? I
have already established has_many belongs_to between blog_images and
posts table.

This doesn’t return error but doesn’t output anything:

post.blog_images.map(&:image_file_name)

Although there are values in the image_file_name field of blog_images
table. And if I was to hard code that value in url, it would work, so
that map isn’t doing what’s expected of it.

ANy suggestions?

thanks for response.

On Fri, Feb 25, 2011 at 11:28 AM, John M. [email protected]
wrote:

post.blog_images.first.image_file_name

outputs

undefined method `image_file_name’ for nil:NilClass

I think that means I called a method on a class that it cannot find?

It means that this particular ‘post’ has no ‘blog_images’ associated
with it; the array is empty so of course ‘first’ is nil …


Hassan S. ------------------------ [email protected]
twitter: @hassan

On 25 February 2011 19:28, John M. [email protected] wrote:

post.blog_images.first.image_file_name

outputs

undefined method `image_file_name’ for nil:NilClass

That means that in your chain of method calls, the thing before
“image_file_name” is nil. So there is no “first” record in the
blog_images collection… are you sure you have some associated
images?

I tend to find using a “guard” helps in chained calls like this:
post.blog_images.first.image_file_name if post.blog_images.first

or even (when necessary)
foo.bar.baz.method_name if foo.bar && foo.bar.baz

On 25 February 2011 21:02, John M. [email protected] wrote:

That means that in your chain of method calls, the thing before
“image_file_name” is nil. So there is no “first” record in the
blog_images collection… are you sure you have some associated
images?

Yes, in my database I have blog_images table with a foreign key of
blog_post_id of 1. In my blog_posts table, I have a record with a
primary key of 1. So there should be at least one association.

Do you know how to use ruby-debug to break into your code and inspect
data. If not have a look at the Rails Guide on debugging. Then you
can break in before the problem line and inspect things to see what is
the problem.

Also you have not said what happens if you include the test checking
for no blog_images.

Colin

Also you have not said what happens if you include the test checking
for no blog_images.

Colin
I think I realized what the problem was. There were four records in the
blog_posts table. At the time of the post, I only had one record in
blog_images table that linked to first record of blog_posts. What this
is doing:

post.blog_images.first.image_file_name

it is iterating for each post the first image_file_name linked to it. So
the time it iterates through the second post, since there is not an
image_file_name associated with it, it throws the exception since the
array object has no image_file_name method!

This was data error on my part. But adding that if is what made me
realize it:

Thanks for all responses.

That means that in your chain of method calls, the thing before
“image_file_name” is nil. So there is no “first” record in the
blog_images collection… are you sure you have some associated
images?

Yes, in my database I have blog_images table with a foreign key of
blog_post_id of 1. In my blog_posts table, I have a record with a
primary key of 1. So there should be at least one association.

Thanks for response