Ruby on rails 什么是';as';在RubyonRails模型中?
RubyonRails模型中的“as”是什么?它是如何工作的 例如Ruby on rails 什么是';as';在RubyonRails模型中?,ruby-on-rails,Ruby On Rails,RubyonRails模型中的“as”是什么?它是如何工作的 例如 has_many :something, as: :reasonable 它是多态的吗?是的,这是一种允许一个模型属于多个模型的方法。应该有 class Something < ApplicationRecord belongs_to :reasonable, polymorphic: true end class Something
has_many :something, as: :reasonable
它是多态的吗?是的,这是一种允许一个模型属于多个模型的方法。应该有
class Something < ApplicationRecord
belongs_to :reasonable, polymorphic: true
end
class Something
然后,任何模型都可以在不添加其他列的情况下合理地使用其中的许多列
class Thing < ApplicationRecord
has_many :somethings, as: :reasonable
end
class Stuff < ApplicationRecord
has_many :somethings, as: :reasonable
end
类事物
某些东西同时存储与它相关联的类和ID,从而允许它是多态的