diff --git a/app/models/concerns/payment_method_distributors.rb b/app/models/concerns/payment_method_distributors.rb index 44973efb92..a6c641f850 100644 --- a/app/models/concerns/payment_method_distributors.rb +++ b/app/models/concerns/payment_method_distributors.rb @@ -9,9 +9,9 @@ module PaymentMethodDistributors extend ActiveSupport::Concern included do - has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', - class_name: 'Enterprise', - foreign_key: 'payment_method_id', - association_foreign_key: 'distributor_id' + has_many :distributor_payment_methods, dependent: :destroy + has_many :distributors, through: :distributor_payment_methods, + class_name: 'Enterprise', + foreign_key: 'distributor_id' end end diff --git a/app/models/distributor_payment_method.rb b/app/models/distributor_payment_method.rb new file mode 100644 index 0000000000..d4cf9f6d1d --- /dev/null +++ b/app/models/distributor_payment_method.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class DistributorPaymentMethod < ApplicationRecord + self.table_name = "distributors_payment_methods" + belongs_to :payment_method, class_name: "Spree::PaymentMethod", touch: true + belongs_to :distributor, class_name: "Enterprise", touch: true +end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index f60ac4500f..9b1f60f457 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -69,10 +69,9 @@ class Enterprise < ApplicationRecord has_many :users, through: :enterprise_roles belongs_to :owner, class_name: 'Spree::User', inverse_of: :owned_enterprises - has_and_belongs_to_many :payment_methods, join_table: 'distributors_payment_methods', - class_name: 'Spree::PaymentMethod', - foreign_key: 'distributor_id' + has_many :distributor_payment_methods, foreign_key: :distributor_id has_many :distributor_shipping_methods, foreign_key: :distributor_id + has_many :payment_methods, through: :distributor_payment_methods has_many :shipping_methods, through: :distributor_shipping_methods has_many :customers has_many :inventory_items diff --git a/db/migrate/20221007103614_add_id_and_timestamps_to_distributor_payment_methods.rb b/db/migrate/20221007103614_add_id_and_timestamps_to_distributor_payment_methods.rb new file mode 100644 index 0000000000..38fc66c966 --- /dev/null +++ b/db/migrate/20221007103614_add_id_and_timestamps_to_distributor_payment_methods.rb @@ -0,0 +1,7 @@ +class AddIdAndTimestampsToDistributorPaymentMethods < ActiveRecord::Migration[6.1] + def change + add_column :distributors_payment_methods, :id, :primary_key + add_column :distributors_payment_methods, :created_at, :datetime + add_column :distributors_payment_methods, :updated_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index a6e1aa230a..7407c758ee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_10_04_165343) do +ActiveRecord::Schema.define(version: 2022_10_07_103614) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -107,9 +107,11 @@ ActiveRecord::Schema.define(version: 2022_10_04_165343) do t.index ["priority", "run_at"], name: "delayed_jobs_priority" end - create_table "distributors_payment_methods", id: false, force: :cascade do |t| + create_table "distributors_payment_methods", force: :cascade do |t| t.integer "distributor_id" t.integer "payment_method_id" + t.datetime "created_at" + t.datetime "updated_at" t.index ["distributor_id"], name: "index_distributors_payment_methods_on_distributor_id" t.index ["payment_method_id"], name: "index_distributors_payment_methods_on_payment_method_id" end