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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:22:24+00:00 2026-05-18T07:22:24+00:00

So I recently created an HABTM relationship between two models (project & user). Before,

  • 0

So I recently created an HABTM relationship between two models (project & user).

Before, I had a user_id column in my project table – that would act like a foreign key. Now there is an entire table that does that.

But how do I reference projects that have a specific user_id & project_id ?

For instance, I used to have a section of my view that looked like this:

<div class="field">
        <%= f.label :project_id %><br />
        <%= collection_select(:stage, :project_id, Project.where(:user_id => current_user), :id, :name) %>
        <br />
    </div>

But how do I now pull the same info from the db, with no model for the HABTM table? The new table is called ‘projects_users’.

My projects model looks like this:

# == Schema Information
# Schema version: 20101125223049
#
# Table name: projects
#
#  id          :integer         not null, primary key
#  name        :string(255)
#  description :string(255)
#  notified    :boolean
#  created_at  :datetime
#  updated_at  :datetime
#

class Project < ActiveRecord::Base

  has_and_belongs_to_many :users
  has_many :stages, :dependent => :destroy
  has_many :uploads
  has_many :comments

  #before_validation { |project| project.user = Authorization.current_user unless project.user }

end

My User Model looks like this:

# == Schema Information
# Schema version: 20101124095341
#
# Table name: users
#
#  id                   :integer         not null, primary key
#  email                :string(255)     default(""), not null
#  encrypted_password   :string(128)     default(""), not null
#  password_salt        :string(255)     default(""), not null
#  reset_password_token :string(255)
#  remember_token       :string(255)
#  remember_created_at  :datetime
#  sign_in_count        :integer         default(0)
#  current_sign_in_at   :datetime
#  last_sign_in_at      :datetime
#  current_sign_in_ip   :string(255)
#  last_sign_in_ip      :string(255)
#  created_at           :datetime
#  updated_at           :datetime
#  username             :string(255)
#  role                 :string(255)
#

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, and :lockable
  devise :database_authenticatable, :registerable, :timeoutable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_and_belongs_to_many :projects
  has_many :stages
  has_many :uploads
  has_many :comments
  has_many :assignments
  has_many :roles, :through => :assignments

  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end  
end

As an aside…how do I edit that table from the rails console – without a model?

Thanks.

  • 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-18T07:22:24+00:00Added an answer on May 18, 2026 at 7:22 am

    I’m not sure I fully understand your question, but Project.where(:user_id => current_user) should become current_user.projects

    And to add user with id 1 to project with id 3 you’d do

    Project.find(3).users << User.find(1)
    

    Is this what you’ve been trying to do ?

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

Sidebar

Related Questions

I've recently created these two (unrelated) methods to replace lots of boiler-plate code in
I started a Rails project recently and decided to use RESTful controllers. I created
I'm a pretty new C# and .NET developer. I recently created an MMC snapin
Recently created a MVC2 Portal but now I want to add a couple of
I recently created a new discussion list in SharePoint for our group, and I
I recently created an xml feed -> html javascript function for an iPhone app
I recently created a Windows service. One thing that it faithfully does is log
I recently created a @sequenceable decorator, that can be applied to any function that
I recently created my first custom control. I attempted to add it in a
I've recently created a new class for my iPhone application which will hold information

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.