簡體   English   中英

has_many通過導軌

[英]has_many through rails

國家模型

set_table_name "countries"

has_many :states, :primary_key => 'col1', :foreign_key => 'col1'

has_many :cities, :through => :states, :primary_key => 'col1', :foreign_key => 'col1'

狀態模型

set_table_name "strain_appendices"  

belongs_to :country

has_many :states, :primary_key => 'col2', :foreign_key => 'col2'

城市模型

set_table_name "ssu_accessions"

belongs_to :country

belongs_to :state

視圖

<% @countries.each do |country| %>

<%= country.high %>

<% country.states.each do |state| %>

<%= state.high %>

<% country.cities.each do |city| %>

<%= city.high %>

<%= country.high %><%= state.high %>提供了不錯的輸出。 但是, <%= city.high %>錯誤顯示為“未初始化的常量Country :: City”。 問題可能出在哪里? 有人幫我嗎

除非您在City模型中定義了一個名為high的方法或沒有high屬性,否則您將收到此錯誤。

也許您正在尋找city.country.highcity.state.high

編輯

對不起,我看錯了! 正如Shadwell所說,在上述情況下您還會遇到另一個錯誤。

在狀態模型中,您缺少此功能:

has_many :cities

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM