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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:38:42+00:00 2026-06-15T15:38:42+00:00

Here are my associations: As user has many projects A project belongs to one

  • 0

Here are my associations:

  • As user has many projects
  • A project belongs to one user

User model (user.rb)

  class User < ActiveRecord::Base
     has_many :projects
     attr_accessible: :available, :department, :name, :skills, :title, :photo
  end

Project modal (project.rb)

  class Project < ActiveRecord::Base
     belongs_to :user, :foreign_key => :user_id
     attr_accessible :project_name
  end

Here’s the code in my show.erb.html file

  <%= @user.projects.each do |project| %>
    <p><%= projects.project_name %></p>
  <% end %>

But this just displays two brackets like this “[]”. Which makes me think it’s an empty array.

So when I run Project.find(1) in rails console to see if the project is being associated with the user, it spits out this:

   => #<Project id: 2, project_name: "This is a project. ", created_at: "2012-11-12 02:29:28", updated_at: "2012-11-12 02:29:28", user_id: nil> 

User id nil? What gives?!

Anyone know what I’m doing wrong? Thank you!

EDIT:

Here is the migration adding the foreign key:

  class AddForeignKeyToProjects < ActiveRecord::Migration
    def change
      add_column :projects, :user_id, :integer
    end
  end

EDIT 2

Here is the instances in the controllers (this is probably the problem)

Project controller

  def new
    @project = Project.new(params[:project], params[:user_id])   
  end

User controller

  def show
    @user = User.find(params[:id], params[:project_name])

    respond_to do |format|
       format.html # show.html.erb
       format.json { render json: @user }
    end
  end
  • 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-15T15:38:43+00:00Added an answer on June 15, 2026 at 3:38 pm

    If you used convention name, it means you had a user_id in Project table, you don’t need to specify foreign_key in Project model, you just need:

    belongs_to :user
    

    I don’t sure if this is your typo:

    <%= projects.project_name %> should be <%= project.project_name %>
    

    About your controller, I suppose you have current_user method, which define current user logged in. So if you want to create new project with user_id associated with current_user’s id, you should write this code:

    def new
      @project = current_user.projects.build # Create new project with user_id = current_user.id
    end
    

    If you want to create new project, your create action will like this:

    def create
      @project = current_user.projects.create(params[:project])
    
      if @project.save # save project success
        # do some thing
      else
        # do some thing
      end
    end
    

    Those are basic actions I can show you, because I don’t know how you build form, and how your route look like.

    When you show a user, if you used RESTful routes, your route will look like this:

    `abc.com/users/:id/show`
    

    What is RESTful routes? If you want to know you should check this Recource routing.

    Then, if you want to find a user, you could use params[:id]:

    def show
      @user = User.find(params[:id])
      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @user }
      end
    end
    

    You can not find a user with params[:project_name], your user model don’t have that attribute, it’s attribute of project.

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

Sidebar

Related Questions

Here are my models and associations: project class Project < ActiveRecord::Base ... has_many :projectmemberizations
I have my models & associations currently setup like so: class User < ActiveRecord::Base
I've got model with many associations. User :has_many => :posts, :videos, :images etc I've
What I have now: class User < ActiveRecord::Base has_many :people end ... and... class
I defined these 3 models in Rails3. class User < ActiveRecord::Base has_many :questions has_many
I've been applying has and belongs to many style associations to a project I've
Here is the relevant data structure and associations: User (has_many completions) - id Quest
I have a User and a Gallery model and the following associations: gallery.rb attr_accessible
I have an association as so: User has many Uploads Uploads belongs to User
I have the table users and scores . Here are the associations: belongs_to :user

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.