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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:50:04+00:00 2026-06-16T00:50:04+00:00

I have a setup where when I user creates a new project they by

  • 0

I have a setup where when I user creates a new project they by default must create a new team that belongs to the project (one-to-one) and a new role that belongs to the team (many-to-one). Everything works as expected except I also want the person who creates the project tied to the first role.

Role therefore has two foreign keys (team_id and user_id). I cannot seem to get user_id to populate like team_id is. Here is the code:

class ProjectsController < ApplicationController
  before_filter :signed_in_user, only: [:show, :edit, :update, :destroy]
  before_filter :correct_or_admin_user,  only: [:edit, :update, :destroy]


  def new
    @user = current_user
    @project = Project.new
    @team = @project.build_team
    @user.roles.build 
    @team.roles.build
  end

  def create
    @project = current_user.projects.new(params[:project])

    if @project.save
      flash[:success] = "Your new project has been created!"
      redirect_to @project
    else
      render 'new'
    end
  end

Is there something wrong with the line “current_user.roles.build? I don’t understand why @team.roles.build is working but not this other line.

  • 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-16T00:50:05+00:00Added an answer on June 16, 2026 at 12:50 am

    http://guides.rubyonrails.org/association_basics.html#has_many-when_are_objects_saved

    I see two problems here:

    1. build does not save the created object. The role you build with the association to the new team gets saved when the team gets saved (when rails knows what the team’s id is, so that it can set the foreign key). Similarly, the team gets saved when the new project gets saved. So those saves all happen when you save the project. But you don’t ever do anything that would result in the role you build with the user association being saved.

    2. Even if you used create (which saves the new associated record), i.e., @user.roles.create, you wouldn’t end up with what I think you want, because you’d be creating a different Role object than the one you get from @team.roles.build.

    Something like the following would solve both of these issues.

    @team.roles << @user.roles.build
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a setup application that if user clicks the same setup twice, they
I have a form that creates a new brand. The user chooses a name,
I have this test case def setUp(self): self.user = User.objects.create(username=tauri, password='gaul') def test_loginin_student_control_panel(self): c
I have a product setup executable that copies some files to the user's hard
I have a User and an Admin role in my project. I created my
I have a Visual Studio setup project. Upon installation, it creates an uninstall batch
I have a Setup Project that I made using Visual Studio 2010. The application
I have setup ability for a user to specify some settings using the in-built
I have setup an anonymous cvs user to be able to check out my
I have the following model setup - a User is interested in projects in

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.