# How to get more informations about an association at runtime

**URL:** <https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187>\
**Category:** Rails\
**Created:** [March 18, 2007, 10:49am UTC](https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187 "2007-03-18T10:49:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 18, 2007, 10:49am UTC](https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187/1 "2007-03-18T10:49:23Z")

</div>

Hi all

I’m working on a Custom Formbuilder. I want it to create me with a  
single method call checkboxes for a given HABTM association.

My model looks like this:

`class Member < ActiveRecord::Base
  has_and_belongs_to_many :preferred_music_styles,
    :class_name => 'MusicStyle',
    :join_table => 'members_have_preferred_music_styles'
end
`

In my view I’d like to do the following:

```auto
<%= form.checkboxes :preferred_music_styles %>
```

The method checkboxes() should now create a set of checkboxes; one for  
each music style.

But for doing this I have to get more informations about the  
“preferred\_music\_styles” association:

1. Does the association exist anyway?
2. Is it a HABTM association anyway?
3. If so, which class (MusicStyle) does it connect to?

I sadly have no idea how to accomplish this task. I’d be very happy if  
some could give me a useful hint where to achieve this information. 🙂

Thanks  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 18, 2007, 11:08am UTC](https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187/2 "2007-03-18T11:08:57Z")

</div>

On Mar 18, 2007, at 10:49 AM, Joshua M. wrote:

> 1. Does the association exist anyway?
> 2. Is it a HABTM association anyway?
> 3. If so, which class (MusicStyle) does it connect to?
> 
> I sadly have no idea how to accomplish this task. I’d be very happy if  
> some could give me a useful hint where to achieve this  
> information. 🙂

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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 18, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187/3 "2007-03-18T14:17:51Z")

</div>

> The key is
> 
> model.class.reflect\_on\_association(association)

Thanks a lot, that’s very helpful. 🙂  
What’s the difference between @class\_name=“MusicStyle” and  
@klass=MusicStyle though?

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?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 18, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187/4 "2007-03-18T16:01:32Z")

</div>

On Mar 18, 2007, at 2:17 PM, Joshua M. wrote:

> > The key is
> > 
> > model.class.reflect\_on\_association(association)
> 
> Thanks a lot, that’s very helpful. 🙂  
> 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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 18, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187/5 "2007-03-18T19:08:07Z")

</div>

> The incantation is:
> 
> real\_object = @template.instance\_variable\_get("@#{@object\_name}")
> 
> – fxn

I am really thankful for your help. I’ve got my method so far:

def checkboxes(assoc\_name, options = {})  
model = @template.instance\_variable\_get("@#{@object\_name}")  
association = model.class.reflect\_on\_association(assoc\_name)  
raise “A has\_and\_belongs\_to\_many association is needed  
(#{association.macro} given)!” unless association.macro ==  
:has\_and\_belongs\_to\_many  
src = [] # Will hold the HTML code for a checkbox per entry  
MusicStyle.find(:all).each do |r|  
name\_param = @object\_name.to\_s + ‘[’ + assoc\_name.to\_s + ‘\_ids][]’  
value\_param = r.id  
checked\_param = model.send(assoc\_name).include?®  
src \<\< # What to do here?  
end  
src.join  
end

I don’t know if all the xxx\_param vars have a correct value, but anyway  
I don’t know how to create the checkbox in a clean way…

A method “check\_box\_tag” is not recognized by the form builder, so what  
to do? I have modified the “check\_box” method so it outputs some HTML,  
but I don’t want to use it because I need just plain checkboxes.

What to do?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 22, 2007, 11:56am UTC](https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187/6 "2007-03-22T11:56:06Z")

</div>

On Mar 18, 2007, at 4:00 PM, Xavier N. wrote:

> > 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}")

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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 18, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187/7 "2007-03-18T19:30:53Z")

</div>

Oh, yet another small question:

I just realized that the checkbox needs a name in the following style:

member[preferred\_music\_style\_ids][]

But whether do I really get why the habtm-name (preferred\_music\_styles)  
is singularized here nor do I see where to get this required form of the  
name from.

At the moment I hacked it together using

name\_param = @object\_name.to\_s + ‘[’ + assoc\_name.to\_s.singularize +  
‘\_ids][]’

(using the singularize method), but I guess this is not very clean, is  
it? Or is it always just the singularized form, also when e.g. the  
singular and plural forms are the same?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 24, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/how-to-get-more-informations-about-an-association-at-runtime/89187/8 "2007-03-24T22:35:35Z")

</div>

Well that’s interesting. Why is the object itself not mandatory and the  
form could evaluate the type class of the object itself? Supplying the  
type AND the object both seems a bit redundant to me…?
