Ruby on rails 3 什么';显示关联数据的最佳方式是什么?
我有3个模型,基因型,Gmarkers和Gsamples,以以下方式关联:Ruby on rails 3 什么';显示关联数据的最佳方式是什么?,ruby-on-rails-3,Ruby On Rails 3,我有3个模型,基因型,Gmarkers和Gsamples,以以下方式关联: class Genotype < ActiveRecord::Base attr_accessible :allele1, :allele2, :run_date belongs_to :gmarkers belongs_to :gsamples end class Gmarker < ActiveRecord::Base attr_accessible :marker has_many
class Genotype < ActiveRecord::Base
attr_accessible :allele1, :allele2, :run_date
belongs_to :gmarkers
belongs_to :gsamples
end
class Gmarker < ActiveRecord::Base
attr_accessible :marker
has_many :genotypes, :dependent => :delete_all
end
class Gsample < ActiveRecord::Base
attr_accessible :box, :labid, :subjectid, :well
belongs_to :gupload
has_many :genotypes, :dependent => :delete_all
end
<% @genotypes.each do |f| %>
<tr>
<td><%= Gmarker.find(f.gmarkers_id).marker %></td>
<td><%= Gsample.find(f.gsamples_id).labid %></td>
<td><%= f.allele1 %></td>
<td><%= f.allele2 %></td>
<td><%= f.run_date %></td>
<td><%= link_to 'Show', f %></td>
<td><%= link_to 'Edit', edit_genotype_path(f) %></td>
<td><%= link_to 'Destroy', f, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
但每当我在控制台中尝试时,我都会从
NameError: uninitialized constant Genotype::Gmarkers
我不明白为什么控制台不知道Gmarkers,因为它们的模型中存在一对多的关系
非常感谢任何帮助
--Rick在您的基因型类中,将您的归属声明更改为:
belongs_to :gmarker
belongs_to :gsample
然后在视图(index.html.erb)上替换以下代码:
<td><%= Gmarker.find(f.gmarkers_id).marker %></td>
<td><%= Gsample.find(f.gsamples_id).labid %></td>
关于这一点:
<td><%= f.gmarker.marker %></td>
<td><%= f.gsample.labid %></td>
<td><%= f.gmarker.marker %></td>
<td><%= f.gsample.labid %></td>