2012-11-05 106 views
1

我是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”吗?我非常感谢你的帮助。

+0

你可以用'rake db:migrate',而不是'rails ...'来迁移数据库。 –

+0

非常感谢。 – jimmy

回答

1

问题是您错误地使用了位置。

由于餐厅has_many的位置,你不能像你提到的方式使用它。因为你有一组位置,实际上是一个ActiveRecord关系,所以为了访问其中一个项目,你必须执行查询并获得其中一个元素。以下是如何获取第一个元素的示例。

restaurant.locations.first.address1 

如果餐馆只有一个位置,应该比你的模式改变为

class Restaurant < ActiveRecord::Base 
    attr_accessible :created, :cuisine_fk, :dish_keywords, :dish_names, :factual_id, :first_name, :last_name, :name, :status 


    has_one :locations 
end 

和访问属性,你正在做的:

restaurant.location.address1 

当然我也会假设你的数据库有你指定的列,否则你将不得不运行迁移。

问候!

1

导轨协会覆盖得很好here in the Rails Guides

我会在这里介绍一个基本设置。

$ rails generate model Restaurant name owner ... 
$ rails generate model Location restaurant_id:integer city ... 

然后,您需要与rake db:migrate迁移数据库的数据库表的更改生效。

的restaurant_id允许我们设置的关联在我们的模型如下

class Restaurant < ActiveRecord::Base 
    has_many :locations, dependent: :destroy 
    attr_accessible :name, :owner 
end 

class Location < ActiveRecord::Base 
    belongs_to :restaurant 
    attr_accessible :city # no restaurant_id here 
end 

现在,您可以通过以下方式访问您的餐厅位置。

r = Restaurant.create!(name: '...') 
l = Location.create!(city: '...') 

# Add association 
r.locations << l 

r.locations will now return an Array with l in it 

l.restaurant will return r 

通过创建新的Rails打了一下与不同风格的关联,例如应用快速只想某种关联,也有一些是需要加入模型。

+0

非常感谢,但我不敢使用“rake db:migrate”,因为我担心这会改变我现有表格中的值。 – jimmy

+0

[Migrations](http://guides.rubyonrails.org/migrations.html)通常不会更改您的数据本身,请更改您的数据库模式以符合您的当前要求。它会查看'$ rails生成模型...'和'$ rails生成迁移...'生成的迁移,只需导航到您的db/migrations文件夹并浏览最近的那些文件。 –

+0

非常感谢,我会再次检查。 – jimmy

0

现在我尝试这种方式,然后它的工作。非常感谢你。

<td> 
     <% restaurant.locations.search(params[:restaurant_fk]).each do |location| %> 
     <!--address = <%= location.address1 %> + " " + <%= location.address2 %>--> 

     <%= location.address1 %> 
     <%= location.address2 %> , 
     <%= location.city %> , 
     <%= location.state %> , 
     <%= location.zip %> 

     <% end %> 
</td>