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 8400937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:40:46+00:00 2026-06-09T21:40:46+00:00

I have two models: class Product < ActiveRecord::Base attr_accessible :amount, :name, :unit, :description, :stock,

  • 0

I have two models:

class Product < ActiveRecord::Base
  attr_accessible :amount, :name, :unit, :description, :stock, :limit
  has_many :cart_rows
  has_one :inquery
end

and

class Inquery < ActiveRecord::Base
  attr_accessible :product, :text, :phone, :zip, :product_id, :state, :street, :city
  belongs_to :product
end

In my index of Inqueries view I have:

<% @inqueries.each do |inquery| %>
  <%= inquery.product.name %>
  <%= inquery.text %>
<% end %>

And I get the following error: undefined method 'name' for nil:NilClass

What am I doing wrong? Does has_one behave differently than has_many?

  • 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-06-09T21:40:47+00:00Added an answer on June 9, 2026 at 9:40 pm

    I built an app based on the info you described here, and was able to reproduce your problem by having an Inquery record with nil values for product (also for text). You may wish to add better validators so that Inqueries with nils cannot be saved.

    Here’s a paste from my rails console session:

      Inquery Load (0.1ms)  SELECT "inqueries".* FROM "inqueries" 
    #<Inquery:0x00000002c03e40>
     => [#<Inquery id: 1, text: nil, phone: nil, zip: nil, product_id: nil, state: nil, street: nil, city: nil, created_at: "2012-08-17 15:23:49", updated_at: "2012-08-17 15:23:49">] 
    
    1.9.3p194 :002 > Inquery.all.each { |n| puts n.product.name }
      Inquery Load (0.2ms)  SELECT "inqueries".* FROM "inqueries" 
    NoMethodError: undefined method `name' for nil:NilClass
    

    As an aside, and now for some unsolicited advice: a field with the same name as a keyword, “text”, is a really bad idea. You might not shoot yourself in the foot today, but you will at some point. Consider renaming the text field to usertext or description or something like that.

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

Sidebar

Related Questions

I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have two models. order and line_item. class Order < ActiveRecord::Base has_many :line_items has_many
I have two models: Product and ProductType: Product: class Product < ActiveRecord::Base default_scope :order
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
I have the following model associations: class Product < ActiveRecord::Base has_many :prices class Price
I have two models: class Product(models.Model): name = models.CharField(max_length=255) class ProductPhoto(models.Model): product = models.ForeignKey('Product',
I have two models class Subscription < ActiveRecord::Base belongs_to :client end class Client <
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =

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.