轨道3:多HAS_ONE协会和放大器;播种 [英] Rails 3: Multiple has_one associations & seeding

查看:81
本文介绍了轨道3:多HAS_ONE协会和放大器;播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与数据概念的Rails似乎并没有做很大的工作带。我终于想通了如何硬code我的外键,使他们成为明智的。

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.

我的模型/ route.rb 是pretty简单:

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.

我的 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")

请注意,我有想告诉种子数据从我创建其他的机场之一走两种不同的方式。无论是一期工程。当我运行耙分贝:种子,所有的 from_ai​​rport_id to_airport_id 字段只设置为1,当机场的ID 表中递增(23安培;在我的当前运行24)。

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).

所以,我有两个问题:


  1. 有没有更好的方式来处理这种模型code比我在做什么?

  2. 我在做什么错在播种: - )

谢谢!

推荐答案

我会改变你的模型指定每个关系的不同的符号:

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

由于启用 HAS_ONE ,您可以通过名称访问的关系(例如 route.airport ),这些都需要是不同的。

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

要得到你的播种工作,调用 .ID 在机场的:

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")

例如:

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">

这篇关于轨道3:多HAS_ONE协会和放大器;播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆