我有3种型号:无法显示选择框
贷款
设备
类别
贷款belongs_to的设备
设备belongs_to的:类
设备的has_many:身外之物
类别has_many:设备
我想用贷款的形式显示一个选择框,但贷款和类别模型之间没有任何关联。我无法看到这两个模型(贷款和类别)之间的关联,但设备和类别是。 我该怎么做?我正在使用简单的表单! 请原谅我的英语!
我有3种型号:无法显示选择框
贷款
设备
类别
贷款belongs_to的设备
设备belongs_to的:类
设备的has_many:身外之物
类别has_many:设备
我想用贷款的形式显示一个选择框,但贷款和类别模型之间没有任何关联。我无法看到这两个模型(贷款和类别)之间的关联,但设备和类别是。 我该怎么做?我正在使用简单的表单! 请原谅我的英语!
已经生成的has_many :through
一个典型例子:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
一个的has_many:通过协会经常被用来建立与其他模型中的许多一对多连接。该关联表明声明模型可以通过继续第三个模型与另一个模型的零个或多个实例进行匹配。例如,考虑患者预约看医生的医疗实践。相关的协会声明可能如下所示:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
在这种情况下,您应该使用委派。将此行添加到您的lending.rb
文件中。
delegate :category, to: :equipment
这将返回有关的类别。