how I can get the has_many associations of a model?
For example if I have this class:
class A < ActiveRecord::Base has_many B has_many C end
I would a method like this:
A.get_has_many
that return
[B,C]
Is it possible? Thanks!
They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.
Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.
You should be using ActiveRecord reflections.
Then you can type something like this:
A.reflect_on_all_associations.map { |assoc| assoc.name}
which will return your array
[:B, :C]
For Example you could try :
aux=Array.new Page.reflections.each { |key, value| aux << key if value.instance_of?(ActiveRecord::Reflection::AssociationReflection) }
Hi Pioz , Have a Nice Day!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With