Ruby on rails 3 哪个活动记录关联更适合这些模型?
我一直在阅读有关铁路指南的活动记录协会的资料,我发现它非常有用,但我确实需要帮助了解一些选择。还在学习 我有一个具有以下属性的客户端模型:Ruby on rails 3 哪个活动记录关联更适合这些模型?,ruby-on-rails-3,activerecord,polymorphic-associations,Ruby On Rails 3,Activerecord,Polymorphic Associations,我一直在阅读有关铁路指南的活动记录协会的资料,我发现它非常有用,但我确实需要帮助了解一些选择。还在学习 我有一个具有以下属性的客户端模型: name birth_date address1 etc auth_num start_date end_date client_id .... I think I need this here for the new contract form code_name status description Contract_id Client_id C
name
birth_date
address1
etc
auth_num
start_date
end_date
client_id .... I think I need this here for the new contract form
code_name
status
description
Contract_id
Client_id
Contract_id
Code_id
Client_id
Units_Alloc ... each contract might use a combination of codes some of which are
the same in other contracts but they have different number of units
allocated.
然后是具有以下属性的合同模型:
name
birth_date
address1
etc
auth_num
start_date
end_date
client_id .... I think I need this here for the new contract form
code_name
status
description
Contract_id
Client_id
Contract_id
Code_id
Client_id
Units_Alloc ... each contract might use a combination of codes some of which are
the same in other contracts but they have different number of units
allocated.
此外,还有一个具有以下属性的代码模型:
name
birth_date
address1
etc
auth_num
start_date
end_date
client_id .... I think I need this here for the new contract form
code_name
status
description
Contract_id
Client_id
Contract_id
Code_id
Client_id
Units_Alloc ... each contract might use a combination of codes some of which are
the same in other contracts but they have different number of units
allocated.
我有一个具有以下属性的联接表ClientLines:
name
birth_date
address1
etc
auth_num
start_date
end_date
client_id .... I think I need this here for the new contract form
code_name
status
description
Contract_id
Client_id
Contract_id
Code_id
Client_id
Units_Alloc ... each contract might use a combination of codes some of which are
the same in other contracts but they have different number of units
allocated.
以及具有以下属性的联接表代码行:
name
birth_date
address1
etc
auth_num
start_date
end_date
client_id .... I think I need this here for the new contract form
code_name
status
description
Contract_id
Client_id
Contract_id
Code_id
Client_id
Units_Alloc ... each contract might use a combination of codes some of which are
the same in other contracts but they have different number of units
allocated.
我很想使用多态关联,因为契约模型与客户机模型和代码模型相关联,但我没有足够的信心在没有首先检查是否有人愿意就我列出的示例给我一些指导的情况下继续进行设置
我希望得到这些问题的指导
多态关联是我上面列出的模型示例的最佳选择吗
由于合同使用不同的代码组合,但有些代码在其他合同中是相同的,唯一的区别是分配的单位数量,我是否在正确的表格中分配了单位?(基本上我不知道把单位放在哪里?)
当我设置数据输入表单,以便我可以输入新合同,从客户表和代码表中提取特定属性时,将客户id列为合同模型的属性之一是否合适?当然,单位分配属性在我心目中仍然是一个大问题,我从哪里取
任何帮助或指示都会非常有用
谢谢。好吧,我试试看:)
我理解,我们来自一份合同,该合同由一名客户签署,涉及一些“代码”。所以它可以是这样的:
class Contract < ActiveRecord::Base
has_one :client
has_many :codes
end
class Client < ActiveRecord::Base
belongs_to :contract
end
class Code < ActiveRecord::Base
belongs_to :contract
end
或者,如果您希望它更复杂,您可以更改与的关系
class Contract < ActiveRecord::Base
has_one :client
has_many :contract_codes
has_many :codes, :through => :contract_codes
end
class Code < ActiveRecord::Base
has_many :contract_codes
has_many :contracts, :through => :contract_codes
end
classcontract:合同\u代码
结束
类代码:合同\u代码
结束
中间模型
ContractCode < ActiveRecord::Base
belongs_to :code
belongs_to :contract
end
ContractCode
您可以在一个特殊列中存储代码的数量。当然,所有这些声明都必须有适当的迁移来支持:)我有点清楚了吗?谢谢@socjopata。。。是的,你确实为我澄清了一些事情,你的回答让我意识到我需要做更多的研究,以便更好地理解这些关系。我可以在为代码、契约和客户机创建模型时创建这些迁移吗?再次感谢,是的,基本上你可以使用rails生成器。当您键入“rails g model Code”时,它将创建一个适当的类,并为其创建示例迁移。