ruby Rails中的双向多语言关联

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Rails中的双向多语言关联相关的知识,希望对你有一定的参考价值。

class Video < ActiveRecord::Base # Image class is similar
  has_many :affixtures, as: :affixable
  with_options through: :affixtures, source: :affix_owner do |assn|
    assn.has_many :articles, source_type: 'Article'
    assn.has_many :products, source_type: 'Product'
  end

  # If you want to get users
  # with_options source: :user do |assn|
  #   assn.has_many :articles_users, through: :articles
  #   assn.has_many :products_users, through: :products
  # end

  # def users
  #   articles_users + products_users
  # end
  # and so on...
end
class Affixture < ActiveRecord::Base
  with_options polymorphic: true do |assn|
    assn.belongs_to :affix_owner
    assn.belongs_to :affix
  end
end

# def change
#   create_table :affixture do |t|
#     t.references :affix_owner, polymorphic: true #, index: true in Rails 4
#     t.references :affixable, polymorphic: true #, index: true in Rails 4

#     t.timestamps
#   end

#   add_index :affixture, [:affix_owner_id, :affix_owner_type]
#   add_index :affixture, [:affixable_id, :affixable_type]
# end
class Article < ActiveRecord::Base # Product class is similar
  belongs_to :user
  has_many :affixtures, as: :affix_owner
  with_options through: :affixtures, source: :affixable do |assn|
    assn.has_many :videos, source_type: 'Video'
    assn.has_many :images, source_type: 'Image'
  end
end

以上是关于ruby Rails中的双向多语言关联的主要内容,如果未能解决你的问题,请参考以下文章

Ruby on Rails 中的表关联

ruby on rails 固定装置中的自动关联

ruby on rails 中的关联

如何正确销毁 ruby​​ on rails 中的关联记录?

下一代的多语言JVM:GraalVM

前后端多语言跨云部署,全链路追踪到底有多难?