cjheath / activefacts-compositions

Create and represent composite schemas, schema transforms and data transforms over a fact-based model
MIT License
1 stars 3 forks source link

Rails model generator generaes duplicate ralationships name #14

Open kirillrdy opened 6 years ago

kirillrdy commented 6 years ago

example cql

vocabulary Warehousing;

Transfer Request ID is written as Auto Counter;
Warehouse ID is written as Auto Counter;

Transfer Request is identified by its ID;

Warehouse is identified by its ID;
Transfer Request is from one Warehouse (as Origin);
Transfer Request is to one Warehouse (as Destination);

running schema_compositor --relational --rails/models Warehousing.cql results in following

module TransferRequest
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'transfer_request_id'

    # Transfer Request is to Destination
    belongs_to :destination, :class_name => 'Warehouse', :foreign_key => :destination_warehouse_id

    # Transfer Request is from Origin
    belongs_to :origin, :class_name => 'Warehouse', :foreign_key => :origin_warehouse_id

  end
end

module Warehouse
  extend ActiveSupport::Concern
  included do
    self.primary_key = 'warehouse_id'

    # Transfer Request is to Destination
    has_many :transfer_requests, :class_name => 'TransferRequest', :foreign_key => :destination_warehouse_id, :dependent => :destroy

    # Transfer Request is from Origin
    has_many :transfer_requests, :class_name => 'TransferRequest', :foreign_key => :origin_warehouse_id, :dependent => :destroy

  end
end

there are two has many transport_requests relationships being generated. could we use roles ( i think they are called roles ) eg transfer_requests_as_origin and transfer_requests_as_destinations it doesnt sound good from english point of view, perhaps this has_many relationship is not useful, but has_many :destinations, :through => :transfer_requests_as_destinations, :source => :destination ( i might have got names wrong ( need cofee )) but basic idea, for a given warehouse, give me all warehouses that I sent stuff to as relationship 1, and give me all warehouses that sent stuff to us as relationship 2.

Hope this makes sense

cjheath commented 6 years ago

Understood. Problem is here https://github.com/cjheath/activefacts-compositions/blob/master/lib/activefacts/compositions/traits/rails.rb#L155 But in playing with the role names, I have an issue that something seems backwards when the foreign key traverses a LinkFactType. Trying to chase it down now.