Ví dụ có một số mô hìnhCó cách nào để kiểm tra rằng liên kết has_many tồn tại trong Rails 3.1 không?
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
Làm thế nào tôi có thể kiểm tra mô hình mà có hiệp hội has_many? Một cái gì đó như thế này
class ActiveRecord::Base
def self.has_many_association_exists?(:association)
...
end
end
Và nó có thể được sử dụng để
Model_1.has_many_association_exists?(:images) # true
Model_2.has_many_association_exists?(:images) # false
Cảm ơn trước
Cảm ơn rất nhiều! Chính xác những gì tôi cần) –