# Has\_many relationships

**URL:** <https://www.ruby-forum.com/t/has-many-relationships/127433>\
**Category:** Rails\
**Created:** [January 29, 2008, 5:43am UTC](https://www.ruby-forum.com/t/has-many-relationships/127433 "2008-01-29T05:43:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![obenhauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d78d45/32.png) [@obenhauser](https://www.ruby-forum.com/u/obenhauser)\
**Post date:** [January 29, 2008, 5:43am UTC](https://www.ruby-forum.com/t/has-many-relationships/127433/1 "2008-01-29T05:43:20Z")

</div>

In my model I have two classes, a donation class and a good class. A  
donation can have mulitple goods, so I set up a has\_many relationship  
between the two objects.

class Donation \< ActiveRecord::Base  
has\_many :goods

class Good \< ActiveRecord::Base  
belongs\_to :donation

In my view (donation/new.html.erb), I add goods to a donation the  
following way;

This will create the following @params after submitting the form.

{“commit”=\>“Submit”,  
“authenticity\_token”=\>“727eec63cfc5b40ee935c0d3478476661893b52c”,  
“donation”=\>{“goods”=\>  
{“1”=\>  
{“item”=\>“books”,  
“description”=\>“Children books”},  
“2”=\>  
{“item”=\>“shoes”,  
“description”=\>“Leather Boots”},  
“3”=\>  
{“item”=\>“blankets”,  
“description”=\>“wool blankets”},}}}

The donation controller:

class DonationsController \< ApplicationController  
def create  
@donation = Donation.new(params[:donation])  
…

So, I was hoping that ruby on rails automatically generates the goods  
and adds them to the donation object, but when create is called, i get  
the following error on the first line;

ActiveRecord::AssociationTypeMismatch in DonationsController#create

Good expected, got Array

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![obenhauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d78d45/32.png) [@obenhauser](https://www.ruby-forum.com/u/obenhauser)\
**Post date:** [January 29, 2008, 7:10am UTC](https://www.ruby-forum.com/t/has-many-relationships/127433/2 "2008-01-29T07:10:40Z")

</div>

Thanks. Is there some way to manipulate the @params info so that ruby  
automates this process?

On Jan 29, 1:21 pm, Chris O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![obenhauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d78d45/32.png) [@obenhauser](https://www.ruby-forum.com/u/obenhauser)\
**Post date:** [January 29, 2008, 6:21am UTC](https://www.ruby-forum.com/t/has-many-relationships/127433/3 "2008-01-29T06:21:39Z")

</div>

There is a lot of magic in rails, but not that much.

Based on the @params info that you showed you will have to extract the  
donation info, create the object and manually make the relations to the  
goods.

ex  
def create  
@donation = Donation.new(params[:donation])  
params[:donation][:goods].each do |g|  
@donation.gifts.build(g)  
end

if @donation.save  
…  
end  
end

Or something like that.

---

<div class="post-metadata">

**Author:** ![obenhauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d78d45/32.png) [@obenhauser](https://www.ruby-forum.com/u/obenhauser)\
**Post date:** [January 29, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/has-many-relationships/127433/4 "2008-01-29T17:42:08Z")

</div>

Franz Obenhauser wrote:

> Thanks. Is there some way to manipulate the @params info so that ruby  
> automates this process?
> 
> On Jan 29, 1:21 pm, Chris O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I think I could explain this if I had to, but let me send you someplace  
better first and if that doesn’t help come back. Railscasts had three  
excelent screen casts on doing exactly what your asking about, and they  
will be much better then a text answer I could write in the next 3  
minutes.

> **[\#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73-complex-forms-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Complex forms often lead to complex controllers, but that doesn’t have  
to be the case. In this episode see how you can create multiple models  
through a single form while keeping the controller clean.

> **[\#74 Complex Forms Part 2 - RailsCasts](http://railscasts.com/episodes/74-complex-forms-part-2)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

See how to use Javascript and RJS to add and remove form fields  
dynamically. This episode will build upon the previous episode allowing  
you to create any number of tasks in one form the same time a project is  
created.

> **[\#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75-complex-forms-part-3)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

In this third and final episode on complex forms [he] will show you how  
to edit a project and multiple tasks all in one form. This includes  
removing and adding tasks dynamically as well.
