我有一个多态关联:

class Option < ActiveRecord::Base
  belongs_to :structurable, polymorphic: true
end

可以连接到人或动物实体。
我想检查一个选项是否有人或动物与之相关我该怎么检查?
o = Option.first
o.structurable == :animal


o.structurable_type == :animal

最佳答案

我想这会有帮助的

o.structurable.class == Animal

或者
o.structurable_type == 'Animal'

10-08 04:30