Skip to content

Instantly share code, notes, and snippets.

@neotericdesign
Last active December 17, 2015 11:09
Show Gist options
  • Save neotericdesign/5599684 to your computer and use it in GitHub Desktop.
Save neotericdesign/5599684 to your computer and use it in GitHub Desktop.
class State < ActiveRecord::Base
has_many :case_type_states
has_many :case_types, :through => :case_type_states
has_many :process_types, :through => :case_type_states
def case_type_processes=(hash)
hash.each do |case_type_id, process_type_ids|
case_type_states.create!(:case_type_id => case_type_id,
:process_type_ids => process_type_ids)
end
end
end
class CaseTypeState < ActiveRecord::Base
belongs_to :state
belongs_to :case_type
has_and_belongs_to_many :process_types
end
# case_type_state_process_types JOIN table (no primary key)
# case_type_state_id, process_type_id
class CaseType < ActiveRecord::Base
end
class ProcessType < ActiveRecord::base
end
case_type_a = CaseType.create! # id => 1
case_type_b = CaseType.create! # id => 2
process_type_a = ProcessType.create! # id => 1
process_type_b = ProcessType.create! # id => 2
state = State.new(:case_type_processes => {
"1" => [2],
"2" => [1]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment