【发布时间】:2011-11-26 20:40:45
【问题描述】:
例如有一些型号
class Model_1 < ActiveRecord::Base
has_many :images, :as => :imageable
end
class Model_2 < ActiveRecord::Base
# doesn't have has_many association
end
...
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
如何检查模型是否具有 has_many 关联?像这样的
class ActiveRecord::Base
def self.has_many_association_exists?(:association)
...
end
end
而且可以这么用
Model_1.has_many_association_exists?(:images) # true
Model_2.has_many_association_exists?(:images) # false
提前致谢
【问题讨论】:
标签: ruby-on-rails activerecord associations