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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:56:31+00:00 2026-05-20T05:56:31+00:00

How can i give composite primary key in Rails without any gem? My first

  • 0

How can i give composite primary key in Rails without any gem?

My first table in migration file:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :userid
      t.string :name
      t.string :address
      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

My second table in migration file:

class CreateProjects < ActiveRecord::Migration
  def self.up
    create_table :projects do |t|
      t.string :title
      t.string :description
      t.timestamps
    end
  end
  def self.down
    drop_table :projects
  end
end

In my schema file:

ActiveRecord::Schema.define(:version => 20110222044146) do
  create_table "projects", :force => true do |t|
    t.string   "title"
    t.string   "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "userid"
    t.string   "name"
    t.string   "address"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
end

Now I want to create a table called User_has_project in which I will refer to User and Project that means will have 2 foreign keys.
So I tried like this:

class CreateUser_has_projects < ActiveRecord::Migration
  def self.up
    create_table :user_has_projects do |t|
      t.references :User
      t.references :Project
      t.boolean :status
      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

Now how can I set combination of user_id and project_id as a primary key in user_has_projects?

  • 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-20T05:56:32+00:00Added an answer on May 20, 2026 at 5:56 am

    It looks like you’re trying to specify a many-many relationship between Users and Projects, with an additional field on the relationship itself.

    The way you’re currently doing isn’t the Rails way of doing things – especially with the concept of a composite primary key.

    The Rails/ActiveRecord way of doing this sort of relationship modelling is to have a third model that describes the relationship between User and Project. For the sake of example, I’m going to call it an Assignment. All you need to do is re-name your user_has_projects table to assignments like so:

    class CreateAssignments < ActiveRecord::Migration
      def self.up
        create_table :assignments do |t|
          t.references :user
          t.references :project
          t.boolean :status
          t.timestamps
        end
      end
    
      def self.down
        drop_table :assignments
      end
    end
    

    And then, in your model files:

    # app/models/user.rb
    class User < ActiveRecord::Base
      has_many :assignments
      has_many :projects, :through => :assignments
    end
    
    # app/models/assignment.rb
    class Assignment < ActiveRecord::Base
      belongs_to :user
      belongs_to :project
    end
    
    # app/models/project.rb
    class Project < ActiveRecord::Base
      has_many :assignments
      has_many :users, :through => :assignments
    end
    

    You can read more about this here: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

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

Sidebar

Related Questions

I have two tables: <class name=Content table=language_content lazy=false> <composite-id> <key-property name=contentID column=contentID/> <key-property name=languageID
any help you can give is very gratefully accepted. I've been looking at this
I'm looking for any tools that can give you code churn metrics (graphs and
The dynamic management views of SQL Server 2005 can give usage information about table
Can you give me an example how to create custom composite control (in Silverlight
Is there any way that i can give a version number to my application
I was wondering if you can give me an advice in creating a table
In my application I have robots that the user can give any number of
In Java you can give the number zero as a single parameter for the
Please give me the advantages and disadvantages of using the particular framework. Can give

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.