0

Still a newbie here, but I still couldn't get the logic right.

Currently, I have:

  1. User has many products.
  2. Product has 1 user with a price attribute.

I am trying to add on:

  1. User can offer 1 price on a product sold by another user. User can offer price on multiple products.
  2. A Product can have many offered price by multiple users.

I have currently come out with:

class User < ActiveRecord::Base has_many :products has_many :offered_prices end class Product < ActiveRecord::Base belongs_to :user has_many :offered_prices end 

This is what I have done so far. It still doesn't seem quite right as I am rather confused at the same time. Your help is very much appreciated! :)

1
  • You need a has_many :through assocation. Take a look at guides.rubyonrails.org/… Commented Apr 6, 2015 at 17:45

1 Answer 1

2

Define three models:

User | OfferedPrice | Product 

The association amongst them will be:

class User < ActiveRecord::Base has_many :products has_many :offered_prices, through: :products end class OfferedPrice < ActiveRecord::Base belongs_to :user belongs_to :product # To make sure a user can offer price once for against a product validates_uniqueness_of :price, scope: [:user, :product] end class Product < ActiveRecord::Base has_many :offered_prices has_many :user, through: :offered_prices end 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.