Существует ли способ узнать, какие ассоциации модель имеет? Возьмите эти 2 модели:
class Comment < ActiveRecord::Base
belongs_to :commentable
end
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
end
Я ищу что-то как:
Post.has_many #=> ['comments', ...]
Post.belongs_to # => ['user']
Comment.belongs_to # => ['commentable']
Вы ищете reflect_on_on_all_associations
.
Короче говоря:
Post.reflect_on_all_associations(:has_many)
...даст массив (объект с атрибутами типа name
и т.д.) всех has_many
ассоциаций.