Rails的两款车型之间的多个关联 [英] Rails multiple associations between two models

查看:171
本文介绍了Rails的两款车型之间的多个关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有飞行,人,和滑翔机模型在Rails 3应用程序。我定义自定义关系,因为我需要一个以上的外键的航班表引用一个人。协会工作,但仅单向。

 班航班及LT;的ActiveRecord :: Base的
  belongs_to的:飞行员:将class_name => 人
  belongs_to的:讲师,:将class_name => 人
  belongs_to的:towplane_pilot,:将class_name => 人
  belongs_to的:airplane_instructor,:将class_name => 人

  belongs_to的:滑翔机
  belongs_to的:rep_glider,:将class_name => 滑翔机

  belongs_to的:departure_airfield,:将class_name => 机场
  belongs_to的:arrival_airfield,:将class_name => 机场

结束

一流的滑翔机<飞机
  的has_many:航班
  的has_many:replaced_flights,:foreign_key => rep_glider_id:将class_name => 飞行
结束

类Person<的ActiveRecord :: Base的
  的has_many:航班,:foreign_key => pilot_id:将class_name => 飞行
  的has_many:instructed_flights,:foreign_key => instructor_id:将class_name => 飞行
  的has_many:towed_flights,:foreign_key => towplane_pilot_id:将class_name => 飞行
  的has_many:instructed_towing_flights,:foreign_key => airplane_instructor_id:将class_name => 飞行
结束
 



  ####什么工程#####
Flight.first.glider
Flight.first.rep_glider
Flight.first.pilot
Flight.first.instructor
Flight.first.towplane_pilot
Flight.first.airplane_instructor

Glider.first.flights
Glider.first.replaced_flights

####有什么不工作#### ----> NoMEthodError匹配
Person.first.flights
Person.first.instructed_flights
Person.first.towed_flights。
Person.first.instructed_towing_flights
 

我几乎有,但我不明白怎么 Glider.first.flights 不工作时, Person.first.flights 不会。

更新:协会以飞机场的作品......所以我无能,为什么它不与人

工作

 类机场<的ActiveRecord :: Base的
  的has_many:takeoff_flights,:foreign_key => departure_airfield_id:将class_name => 飞行
  的has_many:grounded_flights,:foreign_key => arrival_airfield_id:将class_name => 飞行
结束

###工作正常

Airfield.first.takeoff_flights
Airfield.first.grounded_flights

Flight.first.departure_airfield
Flight.first.arrival_airfield
 

解决方案

我一直在说,这些模型之间的关系是否设置正确。

我添加了一个新的记录到航班表,现在的联想与这个新的记录,所有的previous那些正常工作。我真的不知道它是如何工作的,现在,但它肯定不会。

I have Flight, Person, and Glider models in a Rails 3 app. I've defined custom relationships because I need more than one foreign key referencing a Person from the flights table. Associations work but ONE-WAY only.

class Flight < ActiveRecord::Base
  belongs_to :pilot, :class_name => "Person"
  belongs_to :instructor, :class_name => "Person"
  belongs_to :towplane_pilot, :class_name => "Person"
  belongs_to :airplane_instructor, :class_name => "Person"

  belongs_to :glider
  belongs_to :rep_glider, :class_name => "Glider"

  belongs_to :departure_airfield, :class_name => "Airfield"
  belongs_to :arrival_airfield, :class_name => "Airfield"

end

class Glider < Aircraft
  has_many :flights
  has_many :replaced_flights, :foreign_key => "rep_glider_id", :class_name => "Flight"
end

class Person < ActiveRecord::Base
  has_many :flights, :foreign_key => "pilot_id", :class_name => "Flight"
  has_many :instructed_flights, :foreign_key => "instructor_id", :class_name => "Flight"
  has_many :towed_flights, :foreign_key => "towplane_pilot_id", :class_name => "Flight"
  has_many :instructed_towing_flights, :foreign_key => "airplane_instructor_id", :class_name => "Flight"
end



####What works#####
Flight.first.glider
Flight.first.rep_glider
Flight.first.pilot 
Flight.first.instructor 
Flight.first.towplane_pilot
Flight.first.airplane_instructor

Glider.first.flights 
Glider.first.replaced_flights    

####What doesn't work#### ----> NoMEthodError 'match'
Person.first.flights
Person.first.instructed_flights
Person.first.towed_flights.
Person.first.instructed_towing_flights

I'm almost there, but I don't understand how Glider.first.flights does work when Person.first.flights doesn't.

UPDATE: Associations with 'Airfield' works... so I'm clueless as to why it doesn't work with 'Person'

class Airfield < ActiveRecord::Base
  has_many :takeoff_flights, :foreign_key => "departure_airfield_id", :class_name => "Flight"
  has_many :grounded_flights, :foreign_key => "arrival_airfield_id", :class_name => "Flight"
end

###Works Correctly

Airfield.first.takeoff_flights 
Airfield.first.grounded_flights

Flight.first.departure_airfield
Flight.first.arrival_airfield

解决方案

I've been told that the association between these models is set correctly.

I added a new record to the flights table, and now the associations work correctly with this new record and all the previous ones. I'm not really sure how it is working now, but it sure does.

这篇关于Rails的两款车型之间的多个关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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