# Send a array of objects from a view

**URL:** <https://www.ruby-forum.com/t/send-a-array-of-objects-from-a-view/169633>\
**Category:** Rails\
**Created:** [June 6, 2009, 1:13am UTC](https://www.ruby-forum.com/t/send-a-array-of-objects-from-a-view/169633 "2009-06-06T01:13:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsierra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jsierra](https://www.ruby-forum.com/u/jsierra)\
**Post date:** [June 6, 2009, 1:13am UTC](https://www.ruby-forum.com/t/send-a-array-of-objects-from-a-view/169633/1 "2009-06-06T01:13:24Z")

</div>

Hi everyone.

I´m working on application that has a “has\_and\_belongs\_to\_many” betwen  
two models: “expedientes” and “documentos”.  
When I´m on the console, I need to pass a Expedientes Array to  
documentos, otherwise I get a ActiveRecord::AssociationTypeMismatch  
exception.

Example:

> > d=Documento.new  
> > =\> #\<Documento id: nil…
> 
> > d.expedientes=[1]  
> > ActiveRecord::AssociationTypeMismatch: Expediente(#18729340) expected,  
> > got Fixnum(#108610)  
> > …
> 
> > d.expedientes=“1”  
> > ActiveRecord::AssociationTypeMismatch: Expediente(#18729340) expected,  
> > got String(#113650)  
> > …
> 
> > d.expedientes=Expediente.find(“1”)  
> > NoMethodError: undefined method `each’ for #Expediente:0x23a96a0  
> > …
> 
> > d.expedientes=[Expediente.find(“1”)]  
> > =\> [#\<Expediente id: 1, …That´s ok!

How must I made this relationship from the view?  
I´ve mading a Form.hidden\_field from the view “new” with the name  
“expedientes” and the value of the expediente.id of the  
relationship,but seems incorrect.

What´s the best way to do that?

---

<div class="post-metadata">

**Author:** ![jsierra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jsierra](https://www.ruby-forum.com/u/jsierra)\
**Post date:** [June 6, 2009, 2:32am UTC](https://www.ruby-forum.com/t/send-a-array-of-objects-from-a-view/169633/2 "2009-06-06T02:32:19Z")

</div>

The best way (for me) is using nested attributes (http://  
[api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/)  
ClassMethods.html). You can look a very complete example of usage in:  
[GitHub - alloy/complex-form-examples: Simple Rails application which shows the use of NestedAttributes and AutosaveAssociation.](http://github.com/alloy/complex-form-examples/tree/master) and in  
[GitHub - soldieron/jquery-nested-forms: A non-obtrusive solution for dynamic child form adds/subtractions](http://github.com/soldieron/jquery-nested-forms/tree/master)

Regards.

Franco C…

---

<div class="post-metadata">

**Author:** ![jsierra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jsierra](https://www.ruby-forum.com/u/jsierra)\
**Post date:** [June 8, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/send-a-array-of-objects-from-a-view/169633/3 "2009-06-08T15:06:37Z")

</div>

Thanks Franco…
