How to get more informations about an association at runtime

The key is

   model.class.reflect_on_association(association)

Have a look at the classes

   ActiveRecord::Reflection::MacroReflection    ActiveRecord::Reflection::AggregateReflection    ActiveRecord::Reflection::AssociationReflection

for API to query the association, they live in

   activerecord/lib/active_record/reflection.rb

-- fxn

The key is

   model.class.reflect_on_association(association)

Thanks a lot, that's very helpful. :slight_smile: What's the difference between @class_name="MusicStyle" and @klass=MusicStyle though?

In the first assignement there's a string that holds the name of a class. In the second one there's a constant whose value is the very class, that is, an instance of the class Class.

I'm stuck right now with another problem: I need to access the @member object from within my form builder method. Using self.object_name I get "member", but self.object returns nil. Am I looking at the wrong place?

The incantation is:

   real_object = @template.instance_variable_get("@#{@object_name}")

-- fxn

Let me add a comment to that.

The object the form builder is acting upon is not known by the form builder itself, I see no API for that in the current implementation. The default form builder is just a thin wrapper over regular form helper methods, and the logic to figure that out is encapsulated there.

Form builders can optionally receive a second argument with the object itself (passed to form helpers via :object), so the logic to get the actual object would be

   actual_object = object.nil? ?      @template.instance_variable_get("@#{object_name}") :      object

-- fxn