Reputation: 83680
How can I get all relationships for model. IE, I've got User
model:
class User < AR::Base
has_many :messages, :foreign_key => 'author'
has_many :posts
belongs_to :role
end
So how can I know which relationships User
model has got? And foreign_keys if they are presented.
Upvotes: 2
Views: 402
Reputation: 187034
User.reflect_on_all_associations.each do |assoc|
puts "#{assoc.macro} #{assoc.name}"
end
Outputs:
has_many messages
has_many posts
belongs_to role
The reflect_on_all_associations
method return an array of MacroReflection objects. They support other methods as well, for querying the options hash of each association and other useful stuff.
Upvotes: 7