1

Basically, I want to write a web-based curation tool for clinical conditions (disease) and their underlying genetics. Say, I have a clinical condition (class: Phenotype), and I have "mutations"(class: Genotype) that belong to that condition - either individually (i.e. a given mutation is directly causing this condition) or as a group (two or more mutations together cause the condition). Each condition can have one or more of such groups (i.e. may be cause by different mutations or groups of mutations). So I figured I need to create a grouping class (class: GenotypeGroup) to make that association. What I cannot figure out is how to do the form... First, I would want to enter a phenotype with some description. I then would like to use the "show" view to add a new genotype_group to that phenotype (Add new genotype group). This would have to create, implicitly (since it is basically only a cross-reference table) the genotype_group and one or more genotypes which it links to the phenotype entry.

Right now, I have:

class Phenotype
  has_many :genotype_groups
  has_many :genotypes, through: :genotype_groups
  accepts_nested_attributes_for :genotype_groups
end

class GenotypeGroup
  belongs_to :phenotype
  has_many :genotypes
  accepts_nested_attributes_for :genotypes
end

class Genotype
 belongs_to :genotype
end

And zero idea how this would work in terms of nested forms. If anyone has a helpful web resources (been googling for > 1hour now, but apparently don't even know what the thing I am trying to do is called..) - that would be great!

Cheers, M

Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • This question gives some good examples of handling multiple model fields on the same form: https://stackoverflow.com/questions/32884412/how-to-handle-multiple-models-in-one-rails-form – Chris Lewis Aug 08 '18 at 10:45
  • What about use multipart/remote form rendering response with .js.erb? You could have your Phenotype's form, once inserted in you could render it in a create.js.erb view with the form to add your genotypegroup and genotype. – Pietro Allievi Aug 08 '18 at 13:38

1 Answers1

0

Turns out I was missing two things:

a) When nesting the genotype object, I need to add a "Genotype.new" to the nested form element:

<% f.fields_for :genotypes, Genotype.new do |gt| %>
 something_here
<% end %>

b) I had to declare which variables should be carried (i.e. are permitted) by the params object in the respective controller(s) so that I could pass those values between the classes during the nested object creation.