私はRubyonRailsを初めて使用します。既存のデータベースにWebアプリケーションを構築するだけです。私はレールを使用して、レストランとロケーションテーブル用の2つの足場を生成します。その後、これら2つのテーブルの関係を設定します。
class Restaurant < ActiveRecord::Base
attr_accessible :created, :cuisine_fk, :dish_keywords, :dish_names, :factual_id, :first_name, :last_name, :name, :status
has_many :locations
end
class Location < ActiveRecord::Base
attr_accessible :address1, :address2, :city, :created, :latitude, :longitude, :phone, :restaurant_fk, :state, :status, :url, :zip
belongs_to :restaurant
end
これらのテーブルにこの関係を設定した後は、「rake db:migrate」を使用しませんでした。これは、このアクションによって既存のテーブルが変更されるのではないかと心配したためです。
このコマンドラインを実行すると
<%= restaurant.location.address1%>
エラーが表示されます:
undefined method `location'
" NoMethodError in Restaurants#index
Showing C:/Sites/Dishclips/app/views/restaurants/index.html.erb where line #52 raised:
undefined method `location' for #<Restaurant:0x5853bb8> "
その後、ファイルに外部キーを設定しようとしました。
class Location < ActiveRecord::Base
attr_accessible :address1, :address2, :city, :created, :latitude, :longitude, :phone, :restaurant_fk, :state, :status, :url, :zip
belongs_to :restaurant, :class_name => "Restaurant", :foreign_key => 'restaurant_fk'
end
しかし、それでも機能しません。
テーブルの関係を設定した後、「railsdb:migrate」を使用する代わりに外部キーを設定できる方法はありますか?よろしくお願いします。