Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 631973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:01:31+00:00 2026-05-13T20:01:31+00:00

I am trying to populate a nested field, product_name, it an Item. In my

  • 0

I am trying to populate a nested field, product_name, it an Item.

In my Item model:

 class Item < ActiveRecord::Base
   attr_writer :product_name
   belongs_to :order
   belongs_to :product
   def product_name

     #Product.find_by_id(self.product_id) #=> returns the product object
     #self.product #=> returns the product object

     #Product.find_by_id(self.product_id).name #=> complains that 'name' is being called on nil
     #self.product.name #=> complains that 'name' is being called on nil
     #"foooobar" #=> returns "foooobar"

   end     
 end

Each of those commented lines behaves as described.
What I don’t get is, how can something return an object successfully, but then complain that the object is nil when you access an attribute of it?

Here is the whole error:

 NoMethodError in Orders#edit

Showing app/views/orders/_form.haml where line #18 raised:

undefined method `name' for nil:NilClass

Extracted source (around line #18):

17:       = item.hidden_field :product_id
18:       = item.text_field :product_name, :class => 'auto_complete_input'
19:       = item.text_field :quantity, :size => 2

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:01:31+00:00Added an answer on May 13, 2026 at 8:01 pm

    It sounds like you’re asking a really general question. If you’re asking a totally different question than I’m hearing, ignore this. 🙂

    Yes, Ruby is successfully returning an object, and the object is nil. In Ruby, a method can return any value, and nil is just another value that a method can return. Those statements are probably doing a query that isn’t returning any results, so the method returns nil instead of the object you are expecting.

    The longer answer is that Product.find_by_id(x) calls a generated find_by_ attribute method for the id field, which is the same as calling Product.find(:first, :conditions => ["id = ?", x]). If you look at the documentation for .find, you will notice that it says

    Find first – This will return [blah blah black…] If no record can be matched, nil is returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to populate a class object with values from a database table. The
I am trying to populate an instance with LINQ containing an array of nested
I'm trying to create a simple User & Address Model. I've created a nested
I'm trying to populate a table set with more root Locations nested three levels:
I'm trying to get a better setup working for updating a model with nested
I am trying populate H3 with the sum of column E for rows that
I trying to populate the dropdown list when page was loaded.But it is not
I am trying to populate a List<Image> by reading the name of the each
I am trying to populate a ListView ....but I cant add 2 string values
I am trying to populate an array with the text value of a json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.