Skip to content

Instantly share code, notes, and snippets.

@fuzz
Created April 18, 2020 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzz/2ab0fa80fa755e8374f4e95f425269bb to your computer and use it in GitHub Desktop.
Save fuzz/2ab0fa80fa755e8374f4e95f425269bb to your computer and use it in GitHub Desktop.
Offerings
# migration
class CreateOfferings < ActiveRecord::Migration[6.0]
def change
create_table :offerings do |t|
t.references :product, foreign_key: true
t.references :vendor, foreign_key: true
# add other unique-to-vendor fields/associations
t.timestamps
end
end
end
# offering model
class Offering < ApplicationRecord
belongs_to :product
belongs_to :vendor
end
# product model
class Product < ApplicationRecord
has_many :offerings, dependent: :destroy
has_many :vendors, through: :offerings
end
# vendor model
class Vendor < ApplicationRecord
has_many :offerings, dependent: :destroy
has_many :products, through: :offerings
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment