activerecord - Rails - composite primary key as foreign key - relation doesn't work -


i have problem models composite primary key. @ code

migrations

class createdatasets < activerecord::migration   def change     create_table :data_sets |t|       t.integer :synchronization_report_id, null: true       t.string :configuration_id,            null:false, limit: 100       t.datetime :synch_time,               null: false        t.timestamps null: false     end      execute "alter table data_sets drop constraint data_sets_pkey;"     execute "alter table data_sets add constraint data_sets_pkey primary key (id, synchronization_report_id);"     execute "alter table data_sets add foreign key (synchronization_report_id) references synchronization_reports (id);"   end end  class createdataareas < activerecord::migration   def change     create_table :data_areas |t|       t.string :configuration_id,           null: false, limit: 100        t.integer :data_set_id,               null: true       t.integer :synchronization_report_id, null: true        t.string :status,                     null: false, limit: 20       t.string :error_type,                 null: true, limit: 100       t.string :error_text,                 null: true       t.string :sql,                        null: true       t.integer :records_files_processed,   null: false        t.timestamps null: false     end      execute "alter table data_areas drop constraint data_areas_pkey;"     execute "alter table data_areas add constraint data_areas_pkey primary key (id, data_set_id, synchronization_report_id);"     execute "alter table data_areas add foreign key (data_set_id, synchronization_report_id) references data_sets(id, synchronization_report_id);"   end end 

my models

class synchronizationreport::dataset < activerecord::base   self.table_name = :data_sets   self.primary_keys = :id, :synchronization_report_id   belongs_to :synchronization_report   has_many :data_areas, class_name: synchronizationreport::dataarea, dependent: :delete_all   validates_presence_of :synchronization_report_id, :configuration_id, :synch_time   validates_length_of :configuration_id, in: 2..100 end  class synchronizationreport::dataarea < activerecord::base   self.table_name = :data_areas   self.primary_keys = :id, :data_set_id, :synchronization_report_id   belongs_to :data_set, foreign_key: [:data_set_id, :synchronization_report_id]   belongs_to :synchronization_report   validates_inclusion_of :status, in: %w[imported import_failed exported export_failed]   validates_presence_of :configuration_id, :data_set_id, :synchronization_report_id, :status, :records_files_processed   validates_length_of :configuration_id, in: 3..100   validates_length_of :status, in: 3..20   validates_length_of :error_type, in: 3..100, allow_nil: true end 

problem because cannot use data_areas relation.

i cannot pass test.

rspec.describe synchronizationreport::dataset, type: :model   describe "data_areas relation"     before(:all)       (0...5).each |i|         factorygirl.create(:data_area, data_set_id: @data_set.id[0][1], synchronization_report_id: @data_set.synchronization_report.id)       end     end      { should have_many(:data_areas) }     { expect( @data_set.data_areas.size ).to eq 5 }   end end 

last assertion when check relations failure.

 failure/error: { expect( @data_set.data_areas.size ).to eq 5 }     expected: 5         got: 0 

what doing wrong? read topics similar questions on didn't me.

to understand problem created simple app same problem

https://github.com/draqun/composite_primary_keys_as_foreign_key_example

in spec exists test should passed.


Comments