Answer the question
In order to leave comments, you need to log in
How to get a value from a linked table when generating json in a view?
There is a third-party DB. I am accessing it from a rails application. The application receives a parameter by which it searches for data in the database and generates json. Models are described below
class Patient < ActiveRecord::Base
self.table_name = "patient"
establish_connection "pacs_#{Rails.env}".to_sym
has_many :study, :foreign_key => 'patient_fk'
end
class Study < ActiveRecord::Base
self.table_name = "study"
establish_connection "pacs_#{Rails.env}".to_sym
belongs_to :patient #, foreign_key: "patient_fk"
has_many :serieses, :foreign_key => 'study_fk'
end
class Series < ActiveRecord::Base
self.table_name = "series"
establish_connection "pacs_#{Rails.env}".to_sym
belongs_to :study, :foreign_key => 'study_fk'
has_many :instances, :foreign_key => 'series_fk', :primary_key => 'pk'
end
class Instance < ActiveRecord::Base
self.table_name = "instance"
establish_connection "pacs_#{Rails.env}".to_sym
belongs_to :series, :foreign_key => 'series_fk'
belongs_to :files, :foreign_key => 'instance_fk', :primary_key => 'pk'
end
class Files < ActiveRecord::Base
self.table_name = "files"
establish_connection "pacs_#{Rails.env}".to_sym
belongs_to :instance, :foreign_key => 'instance_fk'
end
def images
siuid = params[:study_iuid] unless params[:study_iuid].blank?
@study = Study.find_by study_iuid: siuid
@patient = Patient.find_by pk: @study.patient_fk
@serieses = Series.where(study_fk: @study.pk)
json.extract! @patient, :pat_id, :pat_name
json.extract! @study, :study_iuid, :study_desc, :study_datetime, :num_series, :num_instances
json.seriesList @serieses do |series|
json.(series, :series_iuid, :series_desc, :series_no, :num_instances)
json.instanceList series.instances do |instance|
json.(instance, :sop_iuid)
json.(instance.files, :filepath)
end
end
Answer the question
In order to leave comments, you need to log in
The Files model shouldn't have a foreign_key at all, removed it. Each entry in Instance corresponds to one entry in Files and vice versa. Although initially I considered that one Instance entry corresponded to several entries in Files and the following was written in the Instance model:
And in the view, data from Files was read in a loop
json.filesList instance.fileses do |files|
json.(files, :filepath)
end
Why does the Instance and Files model have the same foreign_key 'instance_fk'? Also, maybe you need to specify in the Files modelhas_many :instance
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question