2012-07-17 100 views
0

我有3種型號,基因型,Gmarkers和G樣本,以下列方式相關聯:顯示關聯數據的最佳方式是什麼?

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 

當我顯示Gentypes(在index.html.erb)的列表,我顯示相關數據以下列方式:

<% @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 %> 

然而,頁面需要一段時間來加載,所以我不知道是否有沒有做每環兩個查找,顯示相關的數據更簡單的方法。我找不到任何相關的數據顯示了使用類似內置引用Rails的風格:

f.GMarker.first.marker 

但每當我嘗試在控制檯中,我得到的錯誤的轉換開始

NameError: uninitialized constant Genotype::Gmarkers 

我不明白乳清控制檯不知道Gmarkers,因爲在他們的模型之間的一個一對多的關係....

任何幫助,非常感謝!

--Rick

回答

0

在你的基因型類,你belongs_to的聲明更改爲:

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> 
相關問題