Ruby on rails Rails:如何将模型与多个父对象关联

Ruby on rails Rails:如何将模型与多个父对象关联,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我的应用程序中有一个笔记模型,我想与其他几个模型共享。比如: rails g模型说明。。。 模型A。。。注:参考文献 rails g模型B。。。注:参考文献 rails g模型C。。。注:参考文献 到目前为止还不错,但我想我想有一种方法——从一个注释——找到“父”模型。请注意,模型是不同的“事物”,因此我不认为我可以在注释模型中做到这一点: belongs_to :model_symbol_here 因为这张便笺可能属于几种不同型号中的一种 从概念上讲,我似乎可以做如下事情: rails g

我的应用程序中有一个笔记模型,我想与其他几个模型共享。比如:

rails g模型说明。。。
模型A。。。注:参考文献
rails g模型B。。。注:参考文献
rails g模型C。。。注:参考文献
到目前为止还不错,但我想我想有一种方法——从一个注释——找到“父”模型。请注意,模型是不同的“事物”,因此我不认为我可以在注释模型中做到这一点:

belongs_to :model_symbol_here
因为这张便笺可能属于几种不同型号中的一种

从概念上讲,我似乎可以做如下事情:

rails g模型注释父项\u id:integer父项\u类型:字符串。。。
我认为这是可行的,但我认为这将以失去利用Rails对模型之间关系的理解的能力为代价


这似乎是一个经常出现的问题,它有一个名称,也有一个“标准”解决方案。有没有合适的“Rails方法”来实现这一点?

生成一个模型父亲

rails g model brand name:string
Brand.create name: 'Apple'
Brand.create name: 'Samsung'
种子模范父亲

rails g model brand name:string
Brand.create name: 'Apple'
Brand.create name: 'Samsung'
生成2个模型子对象

rails g model phone name:string brand:references
rails g model home_appliance name:string brand:references
种子儿童

apple = Brand.first
Phone.create name: 'Iphone X', brand: apple
Phone.create name: 'Iphone 11', brand: apple
HomeAppliance.create name: 'Apple TV', brand: apple
HomeAppliance.create name: 'Homepod', brand: apple

samsung = Brand.last
Phone.create name: 'Galaxy A30', brand: samsung
Phone.create name: 'Galaxy Tab s6', brand: samsung
HomeAppliance.create name: 'Washing machine', brand: samsung
HomeAppliance.create name: 'Refrigerator', brand: samsung
应用内/models/brand.rb

  has_many :phones
  has_many :home_appliances
父亲找儿子

apple.home_appliances
apple.phones
寻子父

phone = Phone.last
phone.brand
添加一个多态示例

加上一个能销售电话和家用电器的推销员

rails g model salesman name:string
销售人员和产品(家电电话)之间的关系是M-M,所以我们需要为此创建第三个表

rails g model product_sold salesman:references product_id:integer product_type:string
应用内/models/product_seld.rb添加

belongs_to :product, polymorphic: true

所以

要查看将要出售的所有内容,请添加app/models/saller.rb

  has_many :product_sold
现在你不能运行这个

products = will.product_sold.map(&:product) 
在产品中,您有一个模型电话和家用电器

rails g model salesman name:string

您发现了另一个多态示例

生成一个模型父亲

rails g model brand name:string
Brand.create name: 'Apple'
Brand.create name: 'Samsung'
种子模范父亲

rails g model brand name:string
Brand.create name: 'Apple'
Brand.create name: 'Samsung'
生成2个模型子对象

rails g model phone name:string brand:references
rails g model home_appliance name:string brand:references
种子儿童

apple = Brand.first
Phone.create name: 'Iphone X', brand: apple
Phone.create name: 'Iphone 11', brand: apple
HomeAppliance.create name: 'Apple TV', brand: apple
HomeAppliance.create name: 'Homepod', brand: apple

samsung = Brand.last
Phone.create name: 'Galaxy A30', brand: samsung
Phone.create name: 'Galaxy Tab s6', brand: samsung
HomeAppliance.create name: 'Washing machine', brand: samsung
HomeAppliance.create name: 'Refrigerator', brand: samsung
应用内/models/brand.rb

  has_many :phones
  has_many :home_appliances
父亲找儿子

apple.home_appliances
apple.phones
寻子父

phone = Phone.last
phone.brand
添加一个多态示例

加上一个能销售电话和家用电器的推销员

rails g model salesman name:string
销售人员和产品(家电电话)之间的关系是M-M,所以我们需要为此创建第三个表

rails g model product_sold salesman:references product_id:integer product_type:string
应用内/models/product_seld.rb添加

belongs_to :product, polymorphic: true

所以

要查看将要出售的所有内容,请添加app/models/saller.rb

  has_many :product_sold
现在你不能运行这个

products = will.product_sold.map(&:product) 
在产品中,您有一个模型电话和家用电器

rails g model salesman name:string

您还发现了另一个多态性示例

如何扩展此功能,使“子”模型可以在多个不同类型的“父”之间共享?例如,将分销商模型添加到手机和家用电器模型中,以便分销商可以找到其产品?如何扩展此功能,使“子”模型可以共享模型可以在多个不同类型的“家长”之间共享-例如,将分销商模型添加到手机和家用电器模型中,以便分销商可以找到其产品?我认为您要寻找的是一种多态关联()。检查一下,希望对你有帮助谢谢!知道这个名字真的很有帮助!我想你想要的是一个多态关联。检查一下,希望对你有帮助谢谢!知道这个名字真的很有帮助!