Rails 3: several has_one associations and seeding

I work with a data concept that Rails does not seem to do very well - Route has two (and only two) airports. I finally figured out how to hard-code my foreign keys so that they are reasonable.

My models/route.rb 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 

All this seems to work fine, but I can't get it to properly seed.

My seeds.rb looks like this:

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

Please note that I have two different ways to try and report seed data from one of the airports that I created for the other. None of them work. When I run rake db:seed , all the from_airport_id and to_airport_id simply set to 1 when the indices in the airport table increase (23 and 24 in my current run).

I have two questions:

  • Is there a better way to handle model code than what I'm doing?
  • What am I doing wrong when sowing :-)

Thanks!

+4
source share
1 answer

I would modify your model to indicate a different character 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 the inclusion of has_one allows you to access this connection through a name (for example, route.airport ), they must be different.

For your seeding to work, call .id at 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"> 
+9
source

Source: https://habr.com/ru/post/1341725/


All Articles