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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:27:46+00:00 2026-06-18T19:27:46+00:00

I have a form from which a project is created. The form collects data

  • 0

I have a form from which a project is created. The form collects data for two tables; ‘projects’ and ‘projects_users’. The last one
is a relational tabel which is used for storing which users that are members of the project (choosen in the form).

Creating a new project works fine, but when it comes to creating new posts in ‘projects_users’ it fails:

uninitialized constant ProjectsController::Projects_users

First of all, am I thinking in the right way (see the code below)?

What I do is that I extract the members array from params[:project], that’s retrieved from the form. Then I’m creating the project in the db, and when that is done I iterate through the members array (which contains of user_id’s) and creates the posts in the db for ‘projects_users’.

What am I doing wrong? Is there a better way to achieve what I want?

projects controller:

class ProjectsController < ApplicationController
def new
  @project = Project.new
  @users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
end

def create
  @members = params[:project].delete(:members)
  @project = Project.new(params[:project].merge(:user_id => current_user.id))

  if @project.save
    @members.each do |member|
        @project_members = Projects_users.new
        @project_members.project_id = @project.project_id
        @project_members.user_id = member.user_id
    end

    redirect_to @project
  else
    @users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
    render :new
  end
end
end

new.html.erb:

<%= form_for @project do |f| %>
<div class="alert alert-block">  
    <%= f.error_messages %>
</div>
<div class="text_field">
    <%= f.label :title%>
    <%= f.text_field :title%>
</div>
<div class="text_field">
    <%= f.label :description%>
    <%= f.text_field :description%>
</div>
<div class="dropdown">
    <%= f.label :start_date%>
    <%= f.date_select :start_date %>
</div>
<div class="dropdown">
    <%= f.label :end_date%>
    <%= f.date_select :end_date %>
</div><br/>
<span class="help-block">Välj användare som ska ingå ingå i projektet.</span>
<div class="checkbox">
    <% @users.each do |user| %>
        <%= check_box_tag "project[members][]", user.id, '1', :id => "user_#{user.id}" %> 
        <%= label_tag "user_#{user.id}", user.first_name + ' ' + user.last_name, :class => "checkbox" %>
    <% end %>
</div>
</div>
<div class="submit">
    <%= f.submit "Spara" %>
</div>
<% end %>

project model:

class Project < ActiveRecord::Base
  has_and_belongs_to_many :users, :class_name => 'User'

  belongs_to :user
  has_many :tickets, :dependent => :destroy

  // validation...

  # Setup accessible (or protected) attributes for your model
  attr_accessible :user_id, :title, :description, :start_date, :end_date
end

users model:

class User < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :email, :password

  has_and_belongs_to_many :projects
  has_many :tickets

  ... other code

end

projects_users table:

project_id
user_id

projects table:

user_id (the user that creates the project = admin)
title
description
start_date
end_date
  • 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-18T19:27:47+00:00Added an answer on June 18, 2026 at 7:27 pm

    In the controller you are using Projects_users.new that means the new method for Projects_users model and as you are using HABTM association the join model doesn’t exist. If you want to use the join/associated model then you must use has_many :through association. But in case you just want to associate preexisting users to projects you can do it following way

    @project.user_ids = @members
    

    OR

    @members.each do |member|
      user = User.find(member)
      @project.users << user
    end
    

    @member will be an array in your case not a hash. It will contain all the checked users’ ids. So you don’t need to have member.user_id as it is not a hash.

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

Sidebar

Related Questions

I have this huge VB project which i just got from some one. i
I have an ASP.NET MVC 2 project in which I've created a data transfer
I have a Main Form which dows calculations and opens and closes projects created
I have a html form which have a select list box from which you
I have a form and from this I call dialogPrintDiet.ShowDialog() which launchs my dialog.
I have a form builder class which inherits from AbstractType and I need to
I have a winforms form which is inherited from another form. e.g. class StartForm
I have this form which allows the input of any product quantity from 1-10:
I have an embedded iframe form which takes a file from a type=file form
I have a main class(which is basically a netbeans form;drag and drop) from where

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.