how to model on rails that a supplier can be a customer and a customer can also be a supplier. ?
Good evening everyone,
Here's my problem:
I have 2 tables, suppliers and customers who are all companies.
how to model on rails that a supplier can be a customer and a customer can also be a supplier. ?
Thank you
Hi, I think I had a simalr issue while dealing with and employee (User) and manger (also User) relationship, my solution was to use a self-jion or self referential association
migration
add_reference :users, :manager, foreign_key: { to_table: :users }, index: true
model
belongs_to :manager, class_name: "User"
Your model is wrong it's better to have a model named partner for example which has a partner type. Now you can seed it with partner types like Vendor, Customer, Supplier Both, whatever you want
class CreatePartnerTypes < ActiveRecord::Migration[5.2]
def change
create_table :partner_types do |t|
t.string :name
t.timestamps
end
add_index :partner_types, :name, unique: true
end
end
class CreatePartners < ActiveRecord::Migration[5.2]
def change
create_table :partners do |t|
t.references :company
t.references :partner_type
t.string :name
t.string :address
t.string :postal_code
t.string :city
t.references :state
t.timestamps
end
end
end