我是Ruby on Rails的新手。我只是在现有的数据库上构建Web应用程序。我使用导轨为餐厅和位置表生成2个脚手架。从那以后,我设置这两个表的关系:在Ruby on Rails上寻找帮助
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
我没有用“耙分贝:迁移”后,我设置了这些表这种关系,因为我害怕,这个动作会更改现有的表。
当我运行此命令行
<%= 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
,但它仍然不工作。
在我们建立表关系之后,有什么办法可以设置外键代替“rails db:migrate”吗?我非常感谢你的帮助。
你可以用'rake db:migrate',而不是'rails ...'来迁移数据库。 –
非常感谢。 – jimmy