I try to wrap my head around a problem I try to tackle in a Ruby on Rails application, but after three days of searching and trying, I seem to get tunnel vision and am stuck:
I have products and shops and a product can be sold by many shops. The prices of that product can differ per shop and I want to create a history of the prices per shop, so I want to save the price information in a separate table.
I created the following migrations:
class CreateProducts < ActiveRecord::Migration[5.2]
def change
create_table :products do |t|
t.string :name
t.text :description
t.string :ean
t.text :category
t.belongs_to :shop, index: true
t.belongs_to :lists, index: true
t.timestamps
end
create_table :shops do |t|
t.string :name
t.string :url
t.integer :priority
t.timestamps
end
create_table :products_shops do |t|
t.belongs_to :products, index: true
t.belongs_to :shops, index: true
t.float :price
t.timestamps
end
end
end
And the following Models:
class Product < ApplicationRecord
belongs_to :shops
end
class Shop < ApplicationRecord
has_many :products
end
My question: How do can I save the price information to the products_shops table? And how do I retrieve the data back with the product, so that I get the product information along with all shops having that product with the most recent price per shop?