E
E
Eugene2015-12-08 11:33:49
Ruby on Rails
Eugene, 2015-12-08 11:33:49

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

Part of the controller code for this view
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)

In the view I do the following
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

The only problem is getting the filepath value, all other data is pulled up. I can not understand why the relationship is not between tables does not work.
The error comes out - undefined method `filepath' for nil:NilClass, i.e. as I understand the association between instance and files does not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2015-12-08
@evanushechkin

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

But this caused an error - undefined method `fileses' for #

N
N. Bekseitov, 2015-12-08
@nbekseitov

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 question

Ask a Question

731 491 924 answers to any question