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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:58:26+00:00 2026-05-16T22:58:26+00:00

I have a users table and a gifts table. Users can pick gifts for

  • 0

I have a users table and a gifts table. Users can pick gifts for their friends. I am thinking of creating a table to store user id and gift id to track which user picked which gift for whom. How to model this relation? I think users and gifts have a HABTM relation. Is that right?

  • 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-16T22:58:26+00:00Added an answer on May 16, 2026 at 10:58 pm

    Try this:

    class User < ActiveRecord::Base
      has_many :user_gifts
      has_many :gifts, :through => :user_gifts  
    
      has_many :rcvd_user_gifts, :class_name =>"UserGift", :foreign_key => :friend_id
      has_many :rcvd_gifts, :through => :rcvd_user_gifts, :source => :gift
    end
    
    class UserGift < ActiveRecord::Base
      # user_id
      # friend_id
      # gift_id
    
      belongs_to :user
      belongs_to :friend, :class_name => "User"
      belongs_to :gift
    
    end
    
    class Gift < ActiveRecord::Base
      has_many :user_gifts
      has_many :senders, :through => :user_gifts, :source => :user
      has_many :receivers, :through => :user_gifts, :source => :friend
    end
    

    Now following calls are possible:

    user.gifts      # Gifts given by a user:
    user.rcvd_gifts # Gifts received by a user:
    gift.senders    # Users sending a gift
    gift.receivers  # Users receiving a gift
    

    Make sure you index the user_id, friend_id, gift_id columns in user_gifts table.

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

Sidebar

Related Questions

I have a Users table in which no two same users can exists ,
I have a Users table, which has a Unique constraint on the username (for
I'm using sql-server 2005 Hi, i have Users table with userID and registrationDate. I
Environment: Rails 3.0.1, MySQL I have a Users table, and I want to search
I have an app in which Users belong to many Categories. So I have
I have a users controller, where if I add a user I want to
I have a CakePHP application that allows a user to register and post projects.
I need to validate a given Name field in the asp.net form. User Name:
I am trying to get to grips with the basics of authentication in Rails.
I need to figure out some clever MySQL snippet that will allow me to

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.