0

ビューはこのコードで大丈夫を示しています

<%= community.country_id %>

<%= community.country.name %>

しかし、これはエラーを返します

'Mysql2 :: Error:Unknown column' countrys.community_id'in' whereclause':SELECT countries。* countries FROMWHERE countriescommunity_id= 5 LIMIT 1 '

私のモデルは

community.rb

has_one :country

country.rb

belongs_to :community
4

1 に答える 1

2

あなたのCountryモデルでは、あなたが次のような関係を持っている場合

has_one :country

デフォルトでは、Railsはcountriesデータベース内のテーブルで。という名前の列を検索しますcommunity_id

発生するエラーは、そのような列をデータベースに移行したことがないということです。シェルから次のコマンドを実行して、その列を追加します。

rails generate migration AddCommunityIdToCountries community_id:integer
rake db:migrate

推奨読書 http://guides.rubyonrails.org/migrations.html

于 2012-12-14T02:21:14.250 に答える