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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:43:06+00:00 2026-05-26T20:43:06+00:00

Hi I have an user model and I also have some fields for their

  • 0

Hi I have an user model and I also have some fields for their information (name, email, bio, etc).

I want to be able to let an user add the products that they have to their account information.

It would only be the names of the products (text only), but I also want to be able to keep count of how many they have overall so I could show something like:

User has 10 products ( product 1, product 2, etc.)

I dont know exactly how go to about doing that. Thanks in advance.

  • 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-26T20:43:07+00:00Added an answer on May 26, 2026 at 8:43 pm

    The next step is: How does this allow a given product, e.g. unique UPC to be purchased by different users?

    After all, a product such as ‘Large Lug Nut’ can be bought by various people.

    So what you probably want is a purchases table which is a basically a join table and has the user_id and the product_id.

    They you can do user.purchases.count

    This is usually done through a has_many :through relationship as in:

    User has_many :purchases
    User has_many :products, :though => :purchases
    
    Purchase belongs_to :user
    Purchase belongs_to :product
    
    Product has_many :purchases
    Product has_many :users, :through => :purchases
    

    Strictly speaking it can also be done through a HABTM (has_and_belongs_to_many !) but this has largely fallen out of favor. It only allow for the two fields and it’s a virtual table that doesn’t exist in the database.
    HABTM has mainly fallen out of favor because as soon as you want to add an additional attribute (very common in the real world) you need to switch to has_many, :through which can be a headache in a fully developed app.

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

Sidebar

Related Questions

I have a user model that requires the user to change their password every
For instance, let's say I have a User model. Users have things like logins,
I have this code in my user model: class User < ActiveRecord::Base attr_accessible :email,
I have a user model that can follow some tags User has_many :tag_followings has_many
I have a Location model with fields: Location(id: integer, name: string, address: string, latitude:
I have two models, user and group. I also have a joining table groups_users.
I have a user model on which I check to make sure that the
I have a User model and a Submission model. Each Submission has a ForeignKey
I have a User model, a Post model, and an Interest model. User has_many
I have a User model, and use an acts_as_authentic (from authlogic) on it. My

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.