Ruby on Rails Tuesday, August 25, 2015

ok... I do not know what a 'STI based model' is.  And I do not see your SecondParty class here.  Pretty sure the matter of second-party can be managed in Party with a field like is_second_party.  Don't understand necessity of Classes Organization and Person

Addressing from what I understand:

class Party < ActiveRecord::Base
   has_many :party_relationships
 
   scope :organizations, -> { where(type: 'Organization') }   ## Haven't tested     scope :people, -> { where(type: 'Person') } ## Haven't tested
 
  ## one to many party to types
  ## SQL table parties should contain field 'types'
  ## rails generate migration add_type_to_parties type:string
  ## rake db:migrate
 
 
  ## <%= form_for @party .. do |f| %>
  ## <%= f.collection_select :type, Party::TYPES, :to_s, :humanize %>
  TYPES =  %w[Organization Person]
 
end



class PartyRelationship < ActiveRecord::Base
   belongs_to :party
   ## http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html
   ## Declare an enum attribute where the values map to integers in the database, but can be queried by name.  So table PartyRelationship must
   ## contain field relation
   enum relation: [ :customer, :supplier, :employee, :reseller ]
   ## eg, corresponding database values : 1:customer, 2:supplier, 3:employee, 4:reseller
   ## With regard to CRUD, obviously the enum is hard-coded.  Pretty sure that all will need to be done manually and very carefully as to respect values
   ## already stored on the database 
   ## With regard to PartyRelationship Controller, record using relation, you will have to be mindful of your enum structure, eg, order
  
  
   ## Find
   ##  irb(main):003:0> PartyRelationship.find(1).relation
   ##  PartyRelationship Load (1.0ms)  SELECT  `party_relationships`.* FROM `party_relationships` WHERE `party_relationships`.`id` = 1 LIMIT 1
   ##  => "supplier"
  
   ## update
   ##  irb(main):008:0> PartyRelationship.find(1).update(relation: 3)
   ##  PartyRelationship Load (0.0ms)  SELECT  `party_relationships`.* FROM `party_relationships` WHERE `party_relationships`.`id` = 1 LIMIT 1
   ## (0.0ms)  BEGIN
   ## SQL (13.0ms)  UPDATE `party_relationships` SET `relation` = 3, `updated_at` = '2015-08-25 14:00:58' WHERE `party_relationships`.`id` = 1
   ## (1.0ms)  COMMIT
   ## => true
 
end

Hope this helps...


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/465ee52d-6771-47f4-b8d8-94aca2cbe388%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment