Ответ 1
Вы ищете reflect_on_all_associations
.
Короче говоря:
Post.reflect_on_all_associations(:has_many)
... даст массив (объекта с атрибутами типа name
и т.д.) всех ассоциаций has_many
.
Есть ли способ узнать, какие ассоциации имеет модель? Возьмите эти 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_all_associations
.
Короче говоря:
Post.reflect_on_all_associations(:has_many)
... даст массив (объекта с атрибутами типа name
и т.д.) всех ассоциаций has_many
.