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

  • Home
  • SEARCH
  • 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 6654729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:25:28+00:00 2026-05-26T01:25:28+00:00

I am trying to have a model Method return information from an associated model.

  • 0

I am trying to have a model Method return information from an associated model. It works without any issues on the Rails console and also outputs the information on the console when it’s running as the web server.

To me it doesn’t seem very difficult. But it’s not working.

class Computer < ActiveRecord::Base
  has_many :ip_addresses
  has_one :status

  def first_ip
    @computer = Computer.find(self.id)
    @computer.ip_addresses.first.ip
    end
end


class IpAddress < ActiveRecord::Base
  attr_accessible :ip
  belongs_to :computers
end

[2011-10-10 16:09:02] ERROR ActionView::Template::Error: undefined method `ip' for nil:NilClass
    /usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.10/lib/active_support/whiny_nil.rb:48:in `method_missing'
    /Users/robertg/RubymineProjects/CMDBTEST/app/models/computer.rb:7:in `first_ip'

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-26T01:25:29+00:00Added an answer on May 26, 2026 at 1:25 am

    Do this:

    class Computer < ActiveRecord::Base
      has_many :ip_addresses
      has_one :status
    
      def first_ip
        first_ip_address = ip_addresses.first
        first_ip_address ? first_ip_address.ip : nil
      end
    end
    

    Using your method, if the Computer has no ip_addresses, then calling .first will return nil, and there is no ip method for NilClass (just like the error says). This way, it checks if there are any ip_addresses, and if so returns the ip of the first ip_address, and if not returns nil.

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

Sidebar

Related Questions

I have a model that I'm trying to retrieve an array of data from,
I am trying to use Windows Workflow and have a model that looks similar
I have a trouble trying to get this working. I have an Item model,
Let's say i have a Domain Model that im trying to make compatible with
Here's what I'm trying to do, and failing... I have a File model which
I have an entry.rb model and I'm trying to make a semi-complicated validation. I
I have installed rapache and I am trying to fit a linear model inside
I have a Spring MVC application trying to use a rich domain model, with
I have a Java project that I'm trying to implement with a model-view-controller design.
I am trying to model a network using C++. I have a struct called

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.