# Has\_many :trough and creating relationships

**URL:** <https://www.ruby-forum.com/t/has-many-trough-and-creating-relationships/186287>\
**Category:** Rails\
**Created:** [April 3, 2010, 6:25pm UTC](https://www.ruby-forum.com/t/has-many-trough-and-creating-relationships/186287 "2010-04-03T18:25:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WSzP](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f14d63/32.png) [@WSzP](https://www.ruby-forum.com/u/WSzP)\
**Post date:** [April 3, 2010, 6:25pm UTC](https://www.ruby-forum.com/t/has-many-trough-and-creating-relationships/186287/1 "2010-04-03T18:25:14Z")

</div>

Hello,  
I got the following application: [http://pastie.org/901794](http://pastie.org/901794) (I pasted  
only the most relevant parts.)  
It gives an error, while I try to submit the form, specifically:  
ActiveRecord::AssociationTypeMismatch in BeiratkozasController#create  
Modul(#51370284) expected, got String(#35011620)

It also does this if I change html \<\< radio\_button(model\_name, method,  
option) to html \<\< radio\_button(model\_name, method, option.id).

It generates the form, and the form works if I comment out the helper,  
so it adds the other fields to the database (those fields are not  
included in the pastie for length issues), but it fails while I wish  
to add the “modul”.

Thanks a thousand times and happy Easter!  
Peter

---

<div class="post-metadata">

**Author:** ![WSzP](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f14d63/32.png) [@WSzP](https://www.ruby-forum.com/u/WSzP)\
**Post date:** [April 3, 2010, 8:12pm UTC](https://www.ruby-forum.com/t/has-many-trough-and-creating-relationships/186287/2 "2010-04-03T20:12:21Z")

</div>

By the way, does anyone know a more complex tutorial on  
has\_many :through, since in books I have, and on the net, there are  
only basic uses, but I can’t find anything that deals with more  
complicated situations, like the one in my previous post.

---

<div class="post-metadata">

**Author:** ![WSzP](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f14d63/32.png) [@WSzP](https://www.ruby-forum.com/u/WSzP)\
**Post date:** [April 3, 2010, 11:19pm UTC](https://www.ruby-forum.com/t/has-many-trough-and-creating-relationships/186287/3 "2010-04-03T23:19:38Z")

</div>

I solved this in a kind of an odd way:

def create  
@student = Student.new(params[:student])  
respond\_to do |format|  
if @student.save  
student\_id = @student.id  
@subscription = Subscription.new(:modul\_id =\>  
params[:valasztott\_training][“moduls”], :student\_id =\> student\_id)  
@subscription.save  
format.html { redirect\_to("/hallgato/"+student\_id.to\_s) }  
else  
format.html { render :action =\> “new” }  
end  
end  
en

---

<div class="post-metadata">

**Author:** ![WSzP](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f14d63/32.png) [@WSzP](https://www.ruby-forum.com/u/WSzP)\
**Post date:** [April 4, 2010, 3:57am UTC](https://www.ruby-forum.com/t/has-many-trough-and-creating-relationships/186287/4 "2010-04-04T03:57:07Z")

</div>

On Apr 3, 5:24 pm, WSzP [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,  
> I got the following application:[http://pastie.org/901794](http://pastie.org/901794)(I pasted  
> only the most relevant parts.)  
> It gives an error, while I try to submit the form, specifically:  
> ActiveRecord::AssociationTypeMismatch in BeiratkozasController#create  
> Modul(#51370284) expected, got String(#35011620)
> 
> It also does this if I change html \<\< radio\_button(model\_name, method,  
> option) to html \<\< radio\_button(model\_name, method, option.id).

The parameters submitted via a form are always strings (or arrays /  
hashes containing strings) - You’ve ended up supplying a string where  
active record expected actual instances of objects.  
In this particular case you might try making your form helpers assign  
to collection\_ids rather than collection

Fred
