John Merlino
2011-Feb-24 03:47 UTC
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</a> <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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Feb-24 04:17 UTC
Re: accessing the field of a model that belongs to another model using dot notation
On Thu, Feb 24, 2011 at 11:47 AM, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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</a> > > <div class="FeatureContent"> > <h2><a href=""><%= post.title %></a></h2> > <p><%= post.body %></p> > <p class="posted"><%= post.user_id %></p> > > </div> > </div> > <% end %> > >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> 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. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Phil Crissman
2011-Feb-24 04:18 UTC
Re: accessing the field of a model that belongs to another model using dot notation
Phil On Wed, Feb 23, 2011 at 9:47 PM, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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</a> > > <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. > >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).> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2011-Feb-25 19:28 UTC
Re: accessing the field of a model that belongs to another model using dot notation
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2011-Feb-25 19:42 UTC
Re: Re: accessing the field of a model that belongs to another model using dot notation
On Fri, Feb 25, 2011 at 11:28 AM, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Feb-25 19:42 UTC
Re: Re: accessing the field of a model that belongs to another model using dot notation
On 25 February 2011 19:28, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> post.blog_images.first.image_file_name > > outputs > > undefined method `image_file_name'' for nil:NilClassThat 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2011-Feb-25 21:02 UTC
Re: Re: accessing the field of a model that belongs to another model using dot notation
> 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Feb-25 21:14 UTC
Re: Re: Re: accessing the field of a model that belongs to another model using dot notation
On 25 February 2011 21:02, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2011-Mar-01 18:04 UTC
Re: Re: Re: accessing the field of a model that belongs to another model using dot notation
> Also you have not said what happens if you include the test checking > for no blog_images. > > ColinI 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: <img src="/images/<%= post.blog_images.first.image_file_name if post.blog_images.first %>" width="220" height="174" alt="" style="top: 27px; left: 13px;" /> Thanks for all responses. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.