# Any way around AssociationTypeMismatch?

**URL:** <https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635>\
**Category:** Rails\
**Created:** [April 2, 2006, 1:20am UTC](https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635 "2006-04-02T01:20:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RailsNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee59a6/32.png) [@RailsNewbie](https://www.ruby-forum.com/u/RailsNewbie)\
**Post date:** [April 2, 2006, 1:20am UTC](https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635/1 "2006-04-02T01:20:51Z")

</div>

I want to have popup menus and check boxes in my forms to let users  
select associated objects. In the form I’m working on, the object  
“belongs\_to” another type of object which is selected from a popup menu.  
The id of the chosen object(s) for association is passed back in the  
parameter hash, but when rails creates a new object from this parameter  
hash, I get a AssociationTypeMismatch. This error seems to be  
complaining that rails expected an actual association object but it  
instead got a string with the object’s id. I’m not sure why rails would  
be expecting the actual associated object to be in the param hash  
arriving from the form, but I’m still relatively new and am probably  
doing something wrong.

Is there a way to instruct rails to expect an id in the param hash of a  
form rather than the actual associated object? I think I could make  
this work by overriding the ActiveRecord::Base update\_attributes or  
attributes= methods, but I’m betting this problem has been worked out  
already since this is such a generic issue.

---

<div class="post-metadata">

**Author:** ![RailsNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee59a6/32.png) [@RailsNewbie](https://www.ruby-forum.com/u/RailsNewbie)\
**Post date:** [April 2, 2006, 1:53am UTC](https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635/2 "2006-04-02T01:53:25Z")

</div>

Additional Info:

I have a class owner which has\_many :item, and a class item which  
belongs\_to :owner

On the form, I have

\<%= select ‘item’, ‘owner’, Owner.find(:all).map{|c| [c.name,c.id]} %\>

---

<div class="post-metadata">

**Author:** ![RailsNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee59a6/32.png) [@RailsNewbie](https://www.ruby-forum.com/u/RailsNewbie)\
**Post date:** [April 2, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635/3 "2006-04-02T18:12:55Z")

</div>

OK, I’ve stumbled upon a solution to the belongs\_to case mentioned  
above. The answer: use owner\_id instead of owner in the form.

I still can’t seem to find the canonical rails way of processing  
has\_and\_belongs\_to\_many associations as represented by a group of check  
boxes yet. Is there a “built in” way to do this easily in rails?

---

<div class="post-metadata">

**Author:** ![RailsNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee59a6/32.png) [@RailsNewbie](https://www.ruby-forum.com/u/RailsNewbie)\
**Post date:** [April 2, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635/4 "2006-04-02T18:30:28Z")

</div>

For HABTM, something like the following should work:

\<%= check\_box\_tag “relationships”, “some\_id”, “checked\_or\_not” %\>  
\<%= check\_box\_tag “relationships”, “some\_other\_id”, “checked\_or\_not” %\>  
\<%= check\_box\_tag “relationships”, “some\_other\_other\_id”,  
“checked\_or\_not” %\>

def update  
…  
@my\_object.habtm\_relationship\_ids = params[:relationships]  
…  
end

That should do it. You’ll need to come up with some logic to  
check/uncheck boxes when the form loads up.

How this works is that the multiple checkboxs all named the same  
passes an array of id’s for your other relationship. Then setting the  
ids field on the object does all the associating for the  
relationships.

Probably a bit confusing, but I hope this helps.  
-Nick

---

<div class="post-metadata">

**Author:** ![RailsNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee59a6/32.png) [@RailsNewbie](https://www.ruby-forum.com/u/RailsNewbie)\
**Post date:** [April 2, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635/5 "2006-04-02T19:35:37Z")

</div>

Thanks for responding. Using the relationship\_ids=array avenue, I’m  
trying to make this more DRY by adding it into ActiveRecord::Base so  
associations will be handled automatically like other attributes during  
calls to “new” or “update\_attributes”. The lines starting with \* are  
the lines I’ve added. It works great on updates of existing records,  
but for reasons still unclear to me it captures some but not all check  
boxes when creating a new object. Am I barking up the wrong tree here?

class ActiveRecord::Base

def attributes=(attributes)  
return if attributes.nil?

- self.class.reflect\_on\_all\_associations.each do |a|
- 

```
  if(attributes[a.name])

```

- 

self.send(Inflector.singularize(a.name.to\_s)+"\_ids=",attributes[a.name.to\_s].map  
{|x| x.to\_i})

- 

```
     attributes.delete(a.name.to_s)

```

- 

```
  end

```

- end

end

---

<div class="post-metadata">

**Author:** ![RailsNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee59a6/32.png) [@RailsNewbie](https://www.ruby-forum.com/u/RailsNewbie)\
**Post date:** [April 2, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635/6 "2006-04-02T19:54:04Z")

</div>

What other ‘automatic’ feautres are looking for? Setting the array ids  
will take care of everything for you. (ie deleting records that are no  
long associated and the like). You shouldn’t need to do anything else.

Setup some test cases and take a look for yourself.

-Nick

---

<div class="post-metadata">

**Author:** ![RailsNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee59a6/32.png) [@RailsNewbie](https://www.ruby-forum.com/u/RailsNewbie)\
**Post date:** [April 2, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635/7 "2006-04-02T20:16:09Z")

</div>

Automatic in the sense that you can just say

@newobj=TheClass.new(param[:theclass])

or

@obj.update\_attributes(param[:theclass])

without having to explicitly hand code for all the associations that the  
object has.

So, in short:  
@obj.update\_attributes(param[:theclass])

instead of

@obj.update\_attributes(param[:theclass])  
@obj.attribute1\_ids=param[:theclass][attribute1]  
@obj.attribute2\_ids=param[:theclass][attribute2]  
@obj.attribute3\_ids=param[:theclass][attribute3]  
@obj.attribute4\_ids=param[:theclass][attribute4]  
@obj.attribute5\_ids=param[:theclass][attribute5]  
etc…

Just like the “new” or “update\_attributes” function allows us to avoid  
typing things such as:  
@obj.name=param[:theclass][:name]  
@obj.address=param[:theclass][:address]  
@obj.phone=param[:theclass][:phone]  
etc…

---

<div class="post-metadata">

**Author:** ![RailsNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee59a6/32.png) [@RailsNewbie](https://www.ruby-forum.com/u/RailsNewbie)\
**Post date:** [April 2, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/any-way-around-associationtypemismatch/56635/8 "2006-04-02T20:19:47Z")

</div>

That lets me be up and running with existing scaffolding code (rails,  
ajax\_scaffold) without having to hand tweak each individual controller  
new/update function to handle associations. I will just have to add the  
check boxes and popup to the views code to handle associations.  
Ideally, the scaffolding code would do that for me too so I can be even  
more lazy than I already am.
