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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:35:05+00:00 2026-06-18T10:35:05+00:00

first of all this is my first Rails application, so please be indulgent… I

  • 0

first of all this is my first Rails application, so please be indulgent… I read the Rails Guides for associations in http://guides.rubyonrails.org/association_basics.html and then started to code my own project. My problem is that I can’t do stuff like @project.user.email because @project.user seems to be nil all the time. This happen for all objects. @user.role.name also throws “undefined method for nil:NilClass”; I’m sure I’m doing wrong someting with the models definitions but I don’t understand what it is. I appreciate your help. Thanks.

   class Role < ActiveRecord::Base
      has_many :users
      attr_accessible :name
   end

   class User < ActiveRecord::Base
      belongs_to :role
      has_many :projects
      attr_accessible :email, :password, :password_confirmation, :role_id, :role  
   end

   class Project < ActiveRecord::Base
      belongs_to :user
      belongs_to :project_type
      attr_accessible :id, :project_type_id, :title, :description  
   end

   class Project_Type < ActiveRecord::Base
      has_many :projects
      attr_accessible :name
   end

An example would be for instance the index view for projects where I do (HAML):

   %td= proyecto.user.email

which wouldn’t work. However,

   %td= proyecto.user_id

does work fine.

  • 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-18T10:35:06+00:00Added an answer on June 18, 2026 at 10:35 am

    When you create a new Project, all of the associations will default to nil unless you have setup some type of default in your migrations. There are a few things you can do there.

    First, you can set the user manually:

    @user = User.find(5)
    @project = Project.new
    @project.user = @user
    

    Additionally, you can build new projects from the user. The build method is added automatically from the has_many association.

    @user = User.find(5)
    @project = @user.projects.build
    

    Now @project will contain a project associated with the user who has id 5. You also need to be sure that you tell Rails what the associations are, otherwise the associations won’t work.

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

Sidebar

Related Questions

First of all this is my markup: <div class=first></div> <div class=second></div> <div class=second></div> <div
first of all: this is not the same as this . The ModelBackend has
first of all this is my third question about web services here and i
First of all: This question is not directly programming related. However, the problem only
Well, first of all sorry about this question it must be pretty straight forward
Maybe it's a similar beginning, but it's true. first of all sorry if this
First of all there is probably a question like this already but i couldn't
First of all I'm not sure this is even possible, however I need to
First of all I don't know if this is the right approach. I want
First of all I did gave a look at this one. But I didn't

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.