Ruby on rails 3 Rails 3:Multiple has_one association&;播种

Ruby on rails 3 Rails 3:Multiple has_one association&;播种,ruby-on-rails-3,associations,seed,Ruby On Rails 3,Associations,Seed,我使用的是Rails似乎并不擅长的数据概念——一条路线有两个(而且只有两个)机场。我终于想出了如何硬编码我的外键,这样外键才有意义 我的models/route.rb非常简单: class Route < ActiveRecord::Base has_one :airport, :foreign_key => 'from_airport_id', :class_name => 'Airport' has_one :airport, :foreign_key =>

我使用的是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
请注意,我有两种不同的方法试图告诉种子数据从我创建的一个机场转到另一个机场。两者都不起作用。当我运行
rake db:seed
时,当
airport
表中的id递增时(在我当前运行中为23和24),所有
from_airport\u id
to_airport\u id
字段都被设置为1

所以我有两个问题:

  • 有没有比我现在做的更好的方法来处理模型代码
  • 我在播种方面做错了什么:-)

  • 谢谢

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

    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
    
    例如:

    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">
    
    ruby-1.9.2-p136:001>a=Airport.create(:icao=>'KPDX',:name=>'Portland International Airport')
    => #
    ruby-1.9.2-p136:002>b=Airport.create(:icao=>'ABCD',:name=>'other Airport')
    => #
    ruby-1.9.2-p136:003>r=Route.create(:to\u airport\u id=>a.id,:from\u airport\u id=>b.id)
    => #
    
    好的,很高兴知道!我做了改变。种子问题看起来还是一样(注意,我没有在seeds.rb中更改任何内容,所以如果我应该更改,我就错过了:-)太好了,这就解决了它。谢谢请注意,其他沿着这条路走的人。。后来我遇到了一些问题,不得不切换到route.rb中的
    属于
    而不是
    有一个
    。做了这些之后,一切都很好。
    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">