Rails 3:多个 has_one 关联 &播种

发布于 2024-10-19 13:00:53 字数 1572 浏览 1 评论 0原文

我正在研究 Rails 似乎不太擅长的数据概念 - 一条路线有两个(而且只有两个)机场。我终于弄清楚如何对我的外键进行硬编码,以便它们变得合理。

我的 models/route.rb 非常简单:

class Route < ActiveRecord::Base
  has_one :airport, :foreign_key => 'from_airport_id', :class_name => 'Airport'
  has_one :airport, :foreign_key => 'to_airport_id', :class_name => 'Airport'
end

这一切似乎都工作正常,但我似乎无法让它正确播种。

我的seeds.rb 看起来像这样:

Airport.delete_all
@kpdx = Airport.create(:icao => 'KPDX', :name => 'Portland International Airport', :lat => '45.58869934', :lon => '-122.5979996')
@ksea = Airport.create(:icao => 'KSEA', :name => 'Seattle Tacoma International Airport', :lat => '47.4490013122559', :lon => '-122.30899810791')
Route.delete_all
Route.create(:from_airport_id => @kpdx, :to_airport_id => @ksea, :route => "RIVR6 BTG OLM6")
Route.create(:from_airport_id => @kpdx, :to_airport_id => @ksea, :route => "BTG OLM OLM6")

Route.create(:from_airport_id => Airport.find_by_icao("KSEA"), :to_airport_id => Airport.find_by_icao("KPDX"), :route => "SEATL4 SEA HELNS4")
Route.create(:from_airport_id => Airport.find_by_icao("KSEA"), :to_airport_id => Airport.find_by_icao("KPDX"), :route => "SEA HELNS4")

请注意,我有两种不同的方法来尝试告诉种子数据从我创建的一个机场到另一个机场。两者都不起作用。当我运行 rake db:seed 时,所有 from_airport_idto_airport_id 字段都设置为 1,此时 airport 表正在递增(在我当前的运行中为 23 和 24)。

所以我有两个问题:

  1. 有没有比我正在做的更好的方法来处理模型代码?
  2. 我在播种时做错了什么:-)

谢谢!

I'm working with a data concept that Rails doesn't seem to do great with - a Route has two (and only two) Airports. I finally figured out how to hard-code my foreign keys so that they would be sensible.

My models/route.rb is pretty simple:

class Route < ActiveRecord::Base
  has_one :airport, :foreign_key => 'from_airport_id', :class_name => 'Airport'
  has_one :airport, :foreign_key => 'to_airport_id', :class_name => 'Airport'
end

This all seems to be working fine but I can't seem to get it to seed correctly.

My seeds.rb looks like so:

Airport.delete_all
@kpdx = Airport.create(:icao => 'KPDX', :name => 'Portland International Airport', :lat => '45.58869934', :lon => '-122.5979996')
@ksea = Airport.create(:icao => 'KSEA', :name => 'Seattle Tacoma International Airport', :lat => '47.4490013122559', :lon => '-122.30899810791')
Route.delete_all
Route.create(:from_airport_id => @kpdx, :to_airport_id => @ksea, :route => "RIVR6 BTG OLM6")
Route.create(:from_airport_id => @kpdx, :to_airport_id => @ksea, :route => "BTG OLM OLM6")

Route.create(:from_airport_id => Airport.find_by_icao("KSEA"), :to_airport_id => Airport.find_by_icao("KPDX"), :route => "SEATL4 SEA HELNS4")
Route.create(:from_airport_id => Airport.find_by_icao("KSEA"), :to_airport_id => Airport.find_by_icao("KPDX"), :route => "SEA HELNS4")

Note that I have two different ways of trying to tell the seed data to go from one of the airports I created to the other. Neither one works. When I run rake db:seed, all of the from_airport_id and to_airport_id fields are just set to 1, when the IDs in the airport table are incrementing (23 & 24 in my current run).

So I have two questions:

  1. Is there a better way to handle the model code than what I'm doing?
  2. What am I doing wrong in seeding :-)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-10-26 13:00:53

我会更改您的模型,为每个关系指定不同的符号:

class Route < ActiveRecord::Base
  has_one :from_airport, :foreign_key => 'from_airport_id', :class_name => 'Airport'
  has_one :to_airport, :foreign_key => 'to_airport_id', :class_name => 'Airport'
end

由于启用 has_one 可以让您通过名称访问该关系(例如 route.airport),因此这些需要有所不同。

要使播种工作正常进行,请在机场致电 .id

Route.create(:from_airport_id => @kpdx.id, :to_airport_id => @ksea.id, :route => "RIVR6 BTG OLM6")

示例:

ruby-1.9.2-p136 :001 > a = Airport.create(:icao => 'KPDX', :name => 'Portland International Airport')
 => #<Airport id: 1, icao: "KPDX", name: "Portland International Airport", created_at: "2011-03-01 02:44:42", updated_at: "2011-03-01 02:44:42">
ruby-1.9.2-p136 :002 > b = Airport.create(:icao => 'ABCD', :name => 'Another Airport')
 => #<Airport id: 2, icao: "ABCD", name: "Another Airport", created_at: "2011-03-01 02:46:22", updated_at: "2011-03-01 02:46:22">
ruby-1.9.2-p136 :003 > r = Route.create(:to_airport_id => a.id, :from_airport_id => b.id)
 => #<Route id: 3, from_airport_id: 2, to_airport_id: 1, route: nil, created_at: "2011-03-01 02:46:36", updated_at: "2011-03-01 02:46:36">

I would change your model to specify a different symbol for each relationship:

class Route < ActiveRecord::Base
  has_one :from_airport, :foreign_key => 'from_airport_id', :class_name => 'Airport'
  has_one :to_airport, :foreign_key => 'to_airport_id', :class_name => 'Airport'
end

Since enabling a has_one lets you access that relationship through the name (e.g. route.airport), these need to be different.

To get your seeding to work, call .id on the airport:

Route.create(:from_airport_id => @kpdx.id, :to_airport_id => @ksea.id, :route => "RIVR6 BTG OLM6")

Example:

ruby-1.9.2-p136 :001 > a = Airport.create(:icao => 'KPDX', :name => 'Portland International Airport')
 => #<Airport id: 1, icao: "KPDX", name: "Portland International Airport", created_at: "2011-03-01 02:44:42", updated_at: "2011-03-01 02:44:42">
ruby-1.9.2-p136 :002 > b = Airport.create(:icao => 'ABCD', :name => 'Another Airport')
 => #<Airport id: 2, icao: "ABCD", name: "Another Airport", created_at: "2011-03-01 02:46:22", updated_at: "2011-03-01 02:46:22">
ruby-1.9.2-p136 :003 > r = Route.create(:to_airport_id => a.id, :from_airport_id => b.id)
 => #<Route id: 3, from_airport_id: 2, to_airport_id: 1, route: nil, created_at: "2011-03-01 02:46:36", updated_at: "2011-03-01 02:46:36">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文