【发布时间】:2016-10-24 22:48:06
【问题描述】:
以rubyonrails.org 中的多态关联示例并使用以下模型:
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
class Employee < ActiveRecord::Base
has_many :pictures, as: :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, as: :imageable
end
如何找到有效的imageable_types?
例如以便它返回:[:employee, :product]
【问题讨论】:
-
6年前就有这个答案stackoverflow.com/questions/2315239/…。它是什么循环遍历项目中的每个模型并使用方法 reflect_on_all_associations 查找 has_many 或 has_one 关联。
标签: ruby-on-rails ruby-on-rails-4 associations polymorphic-associations