Reputation: 39
I am using Ruby on Rails 4.0.1 and I would like to get the array of associated models. That is, I have the following models and associations:
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
has_many :likes, dependent: :destroy
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class Like < ActiveRecord::Base
belongs_to :post
end
And I would like to get the array of associated models likes following.
Post.has_many_associated
=> [:comments, :likes]
Any Idea?
Thank you for your advice.
Tae-ho.
Upvotes: 0
Views: 286
Reputation: 15781
Yes, you can do this with reflect_on_all_associations
:
Post.reflect_on_all_associations(:has_many).map(&:name)
Upvotes: 1