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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:35:59+00:00 2026-06-01T07:35:59+00:00

I have 3 models. These are the User , Product and Price model. I

  • 0

I have 3 models. These are the User, Product and Price model. I want Users to be able to subscribe to Products and watch the Price model for lower prices. I’m thinking about how to set this up but haven’t figured out the correct associations.

My associations as of now are like this:

Note: Many fields (table columns as well) have been erased for simplicity

class User
  has_many :prices, :dependent => :destroy
  has_many :products, :through => :prices
end

class Product
  # table columns - :name
  has_many :prices
  has_many :users, :through => :prices
end

class Price 
  # table columns - :cost, :user_id, :product_id, :store_id
  belongs_to :user
  belongs_to :product
  belongs_to :store
end

I was going to set this up by making a boolean in the Product model called :watch_product but I get stuck at the associations. Should I give Product a user_id or the other way around? I have a :through association but no user_id on Product or a product_id on User. Should the watch_product go on the Price instead?

  • 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-01T07:36:00+00:00Added an answer on June 1, 2026 at 7:36 am

    I would not store the user information in the Price model. I would introduce a separate model for storing user to product subscription.

    class User
      has_many :user_products
      has_many :products, :through => :user_products
      has_many :prices, :through => :products 
    end
    
    class UserProduct
      belongs_to :user
      belongs_to :product
    end
    
    class Product
      has_many :prices
      has_many :user_products
      has_many :users, :through => :user_products
    end
    
    class Price 
      # table columns - :cost, :product_id, :store_id
      belongs_to :product
      belongs_to :store
    
      scope :by_product, lambda {|product| where(:product_id => product)}
      scope :by_store, lambda {|store| where(:store_id => store)}
    end
    

    Now you can get the prices for user as follows:

    user.prices.by_product(12)
    user.prices.by_store(10)
    user.prices.by_product(12).by_store(10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these models: class UserProfile(models.Model): user = models.OneToOneField(User) #etc class Organization(models.Model): users =
I have these models: class Model1(models.Model): ... class Model2(models.Model): m1 = models.OneToOneField(Model1, related_name='m2') ...
I have these models: class Gallery(models.Model): HeadImage = models.ImageField(upload_to=gallery,blank=True,null=True) class News(Gallery): Name=models.CharField(max_length=100) #some other
Let's say I have these models: class A(models.Model): name = models.CharField(max_length=32) b = models.ForeignKey(B,
I have these simple classes Class A(models.Model): ... Class Meta(models.Model): a = models.OnetoOneField(A, primary_key=True)
I have three models: User, Product, Offer and a problem with the relationship between
Let's say I have these tables/models: Product - id - last_updated_date - name -
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have three models ( User , Tag , Product ) and they interact
I have this models: class Comment(models.Model): text = models.TextField(max_length = 300) author = models.ForeignKey(User)

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.