Sorry, Leandro, but I do not agree with you. However I may not understand what you have offered. Just seems to me that the solution is model-drive. Where in the Party and PartyRelationship controller aspects of CRUD can be managed in building respective association calls. Like << to create, .destroy to delete, etc.. Maybe at a maintenance level Role/Customer and CustomerType/Supplier Controllers would be appropriate
(Forgive I am writing quickly)
I would suggest a model structure like the following:
class Party < ActiveRecord::Base
has_many :party_relationships
has_many :roles
end
class Role < ActiveRecord::Base
belongs_to :party_relationship
end
class CustomerType < ActiveRecord::Base
belongs_to :party_relationship
end
class PartyRelationship < ActiveRecord::Base
belongs_to :party
## customer, supplier, reseller
scope :customer, -> { where(customer_type_id: 1) } ## id values are specific as to new table/record creation and to my seed below
scope :supplier, -> { where(customer_type_id: 2) }
scope :reseller, -> { where(customer_type_id: 3) }
has_many :customer_types
end
Offering seed.rb
## Assuming you are starting anew
unless Party.count > 0
Party.create!(name: 'Person#1', role_id: 2 ) ## id: 1
Party.create!(name: 'Person#2', role_id: 2 ) ## id: 2
Party.create!(name: 'Vendor#1', role_id: 3 ) ## id: 3
Party.create!(name: 'Vendor#2', role_id: 3 ) ## id: 4
Party.create!(name: 'Company#1', role_id: 1 ) ## id: 5
Party.create!(name: 'Company#2', role_id: 1 ) ## id: 6
end
unless PartyRelationship.count > 0
PartyRelationship.create!(party_id: 1, customer_type_id: 1 ) ## Person 1 is an Person that is a customer
PartyRelationship.create!(party_id: 2, customer_type_id: 2 ) ## Person 2 is an Person that is a supplier
PartyRelationship.create!(party_id: 3, customer_type_id: 3 ) ## Vendor 1 is an Companu that is a reseller
PartyRelationship.create!(party_id: 4, customer_type_id: 1 ) ## Vendor 2 is an company that is a customer
PartyRelationship.create!(party_id: 5, customer_type_id: 2 ) ## Company 1 is an organization that is a supplier
PartyRelationship.create!(party_id: 6, customer_type_id: 3 ) ## Company 2 is an organization that is a reselller
end
PartyRelationship.create!(party_id: 1, customer_type_id: 2 ) ## Person 1 is an Person that is a supplier
unless Role.count > 0
Role.create!(role_name: 'Organization')
Role.create!(role_name: 'Person')
Role.create!(role_name: 'Company')
end
unless CustomerType.count > 0
CustomerType.create!(name: 'customer')
CustomerType.create!(name: 'supplier')
CustomerType.create!(name: 'reseller')
end
And migration:
create_table "parties", force: :cascade do |t|
t.integer "role_id", limit: 4
t.string "name", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "party_relationships", force: :cascade do |t|
t.integer "party_id", limit: 4
t.integer "customer_type_id", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "roles", force: :cascade do |t|
t.string "role_name", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "customer_types", force: :cascade do |t|
t.string "name", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Then you should be able to make association calls like
Party.find(1).party_relationships.customer
There may be many errors here, but the spirit is that one can perform CRUD given the Party and PartyRelationship model through their respective controllers..... FORGIVE me if I am wrong.
Liz