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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:02:55+00:00 2026-06-02T00:02:55+00:00

Models: class Car < ActiveRecord::Base … belongs_to :manufacturer … end class Manufacturer < ActiveRecord::Base

  • 0

Models:

class Car < ActiveRecord::Base
  ...
  belongs_to :manufacturer
  ...
end


class Manufacturer < ActiveRecord::Base
  ...
  has_many :cars
  ...
end

Car controller:

  def index
     @title = "All cars"
     @cars = Car.paginate(:page => params[:page], :per_page => 20)
  end

View:

<%= car.manufacturer.name %>

Shows: undefined method `name’ for nil:NilClass

Ok. View:

<%= car.manufacturer[name] %>

Shows: undefined local variable or method `name’ for #<#:0x460c488>

BUT! View:

<%= car.manufacturer.to_yaml %>

Shows:

--- !ruby/object:Manufacturer
attributes:
  id: 1
  name: Acura
  created_at: 2011-11-30 09:59:19.750976000 Z
  updated_at: 2011-12-06 10:38:46.569531000 Z

So how can i read name of Car.manufacturer?

  • 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-02T00:02:57+00:00Added an answer on June 2, 2026 at 12:02 am

    <%= car.manufacturer.name if car.manufacturer %> as some results will not have the attribute set, this ensures you only call the attribute getter method if it has been set.

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

Sidebar

Related Questions

Two models: class Task < ActiveRecord::Base has_many :subtasks end class Subtask < ActiveRecord::Base belongs_to
I have these 3 models: class Car < ActiveRecord::Base belongs_to :user has_many :car_styles has_many
I have 2 models: # models/car.rb class Car < ActiveRecord::Base belongs_to :model end and
I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class
if I have a ActiveRecord model: class Car < ActiveRecord::Base belongs_to :store end In,
INTRO model A carname.rb class Carname < ActiveRecord::Base belongs_to :car has_many :carmodels, :dependent =>
my models class Auction belongs_to :item belongs_to :user, :foreign_key => :current_winner_id has_many :auction_bids end
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have a model with the following associations: class Car < ActiveRecord::Base belongs_to :transportation_vehicle

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.