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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:20:02+00:00 2026-05-31T19:20:02+00:00

I have a User.rb model and a Donation.rb model. User.rb has_one :donation , and

  • 0

I have a User.rb model and a Donation.rb model. User.rb has_one :donation, and Donation.rb belongs_to :user.

I created a form whereby a donation gets linked to a user. In the console, I’m able to do this

ruby-1.9.2-p290 :003 > user.donation.amount
 => 1 

Similarily, I’m able to do this on the views/users/show.html.erb

<p>Donation:<%= @user.donation.amount if @user.donation.amount? %></p>

However, in views/home/index.html.erb, I’m doing this

<h3>Home</h3>
<% @users.each do |user| %>
  <p>User: <%=link_to user.name, user %></p>
  <p>Email: <%= user.email if user.email %></p>
  <p>Name:<%= user.name %></p>
  <p>Donation:<%= user.donation.amount if user.donation.amount? %></p>

and getting this error

    NoMethodError in Home#index

Showing /Users/michaeljohnmitchell/Sites/leafpledge/app/views/home/index.html.erb where line #6 raised:

undefined method `amount?' for nil:NilClass

I’m assuming this is because the association between User.rb and Donation.rb doesn’t work with the index action in the home_controller.rb because every user is being fetched rather than the particular user that I have in the views/users/show.html.erb.

 def index
    @users = User.all
  end

Any idea how I can get it to work?

  • 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-31T19:20:03+00:00Added an answer on May 31, 2026 at 7:20 pm

    Your if check should be checking if the donation exists, not if the amount exists:

    <p>Donation:<%= user.donation.amount if user.donation %></p>
    

    Or, you could use the try method which will avoid the undefined method error if donation is nil:

    <p>Donation:<%= user.donation.try(:amount) %></p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user model and a profile model. user has_one :profile profile belongs_to
I have a user model with has_one :designpad , a designpad model with belongs_to
I have a donation script at this moment where the user inputs the donation
I have simple user model: var user = Backbone.Model.extend({ initialize: function(){ this.bind(change:auth, function (){
We have a user model which :has_one detail . In a form_for a user,
I have User model and Group model. User belongs_to Group . Group has an
I have a user model that has_one profile, and the profile belongs to the
I have a user model like this: class User < ActiveRecord::Base validates :password, :presence
I have a User model and a Job_Category model, the Job_Category model belongs_to :user
Ok i have this in what i have user.rb //user model OPTION = ['none',

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.