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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:58:39+00:00 2026-05-27T05:58:39+00:00

I have a rails app with two models, visits and users. A visit has

  • 0

I have a rails app with two models, visits and users. A visit has 2 users; a controller and an engineer. I’ve set up the relationships as follows, but for some reason I can’t access the attributes of the related User. Is this because I am using devise?

Visit Model

class Visit < ActiveRecord::Base
  belongs_to :engineer, :class_name => "User", :foreign_key => 'engineer_id'
  belongs_to :controller, :class_name => "User", :foreign_key => 'controller_id'
end

User Model

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_many :engineer_visits, :class_name => "Visit", :foreign_key => "engineer_id"
  has_many :controller_visits, :class_name => "Visit", :foreign_key => "controller_id"
end

Visits index view

<% @visits.each do |visit| -%>
  <%= visit.visit_date.strftime("%a %d/%m/%Y") %>
  <%= visit.engineer.email %>
<% end -%>

This is where it fails, with the following exception:

NoMethodError in Visits#index
...
undefined method `email' for nil:NilClass

If I print the <%= debug visit.engineer %> there is an email attribute, but I can’t actually get any of the individual attributes to display.

Any ideas on how I can access these variables?

  • 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-27T05:58:40+00:00Added an answer on May 27, 2026 at 5:58 am

    Maybe that visit you are getting the error for, is a visit of a controller, not of an engineer.

    try modifying your view like:

    <% @visits.each do |visit| -%>
      <%= visit.visit_date.strftime("%a %d/%m/%Y") %>
      <%= visit.engineer.email unless visit.engineer.nil? %>
      <%= visit.controller.email unless visit.controller.nil? %>
    <% end -%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails 2.3.11 app which has two key Models: Activity Transaction Live
I have a rails app that has two models one is vender and the
for example a have simple rails app with two models, users and books. now
I have RailsAdmin running for my Rails 3 app which has two models -
I have a rails app that has picked up a bit of traction, but
I have a simple user registration page in my rails app that has two
I'm writing a Rails app in which I have two models: a Machine model
I currently have a Rails 3.0.10 app and I made two models with simple
In my Rails app, I have two models, Estimate and Client , which both
My rails app has two pages based on two controllers/ views/ models..both controllers use

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.