# I'm about to give up...I guess noone can help with passing arrays between the controller and view?

**URL:** <https://discuss.rubyonrails.org/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-arrays-between-the-controller-and-view/8846>\
**Category:** rubyonrails-talk\
**Created:** [February 16, 2007, 6:21pm UTC](https://discuss.rubyonrails.org/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-arrays-between-the-controller-and-view/8846 "2007-02-16T18:21:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick8](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rick8](https://discuss.rubyonrails.org/u/Rick8)\
**Post date:** [February 16, 2007, 6:21pm UTC](https://discuss.rubyonrails.org/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-arrays-between-the-controller-and-view/8846/1 "2007-02-16T18:21:46Z")

</div>

No one seems to be able to help with my request. I bet this can be done in rails, but I'm not going to figure it out without any help anytime soon.

Trying to be brief: I have three classes: Account, Employee, and AccountEmployeeRelation (which contains more than just the FKs). In the form where I create a new account, I know how to add a ONE linked employee. I do not know how to extend this to add MULTIPLE linked employees. Here is the working code for one employee:

&nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@account = Account.new &nbsp;&nbsp;&nbsp;&nbsp;@relation = AccountEmployeeRelation.new &nbsp;&nbsp;end

&nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@account = Account.new(params[:account]) &nbsp;&nbsp;&nbsp;&nbsp;@relation = AccountEmployeeRelation.new(params[:relation]) &nbsp;&nbsp;&nbsp;&nbsp;@account.account\_employee\_relations \<\< @relation &nbsp;&nbsp;&nbsp;&nbsp;@account.save &nbsp;&nbsp;&nbsp;&nbsp;@relation.save &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> :list &nbsp;&nbsp;end

The View: \<% form\_for :account, :url =\> { :action =\> :create } do |form| %\> Title: \<%= form.text\_field :title %\>\<br/\>

\<% fields\_for :relation do |f|%\> &nbsp;&nbsp;&nbsp;Employee: \<%= f.select (:employee\_id, Employee.find(:all).map{|u| [u.name, u.id]}) %\>\<br/\> \<% end %\>

\<%= submit\_tag %\> \<% end %\>

Can anyone help with extending this? I think new needs to look like: &nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@relations = &nbsp;&nbsp;&nbsp;&nbsp;3.times do # I would not really do it this way. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@relations \<\< AccountEmployeeRelation.new &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

But I'm stuck on the view.

---

<div class="post-metadata">

**Author:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [February 16, 2007, 6:40pm UTC](https://discuss.rubyonrails.org/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-arrays-between-the-controller-and-view/8846/2 "2007-02-16T18:40:29Z")

</div>

Rick Schumeyer wrote:

> &nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@account = Account.new &nbsp;&nbsp;&nbsp;&nbsp;@relation = AccountEmployeeRelation.new

I will suggest something really sucky. Either it will work brilliantly, or you will get your answer from others leaping to your defense from me.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.times do |idx| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval "@relations\_#{idx} = AccountEmployeeRelation.new" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end ...

> &nbsp;&nbsp;&nbsp;&nbsp;@account = Account.new(params[:account])

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.times do |idx| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;relation = AccountEmployeeRelation.new(params[:"relation\_#{idx}"]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;account.account\_employee\_relations \<\< relation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end ...

> The View: \<% form\_for :account, :url =\> { :action =\> :create } do |form| %\> Title: \<%= form.text\_field :title %\>\<br/\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.times do |idx| # similar symbol-abuse here...

\<% fields\_for :"relation\_#{}" do |f|%\>

> &nbsp;&nbsp;&nbsp;Employee: \<%= f.select (:employee\_id, Employee.find(:all).map{|u| [u.name, u.id]}) %\>\<br/\> \<% end %\>

And so on. However, I suspect in general you user interface is too-closely matching the exact shape of your database. It should match the way your users will typically enter their data.

---

<div class="post-metadata">

**Author:** ![Michael\_Kovacs](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@Michael\_Kovacs](https://discuss.rubyonrails.org/u/Michael_Kovacs)\
**Post date:** [February 16, 2007, 6:57pm UTC](https://discuss.rubyonrails.org/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-arrays-between-the-controller-and-view/8846/3 "2007-02-16T18:57:10Z")

</div>

Rick,

You can set multiple values on a collection like this:

@account.account\_employee\_ids = params[:relation\_ids]

where params[:relation\_ids] is an array of the relationship ids.

I wrote up an example in my blog awhile back:

[http://javathehutt.blogspot.com/2006/08/rails-realities-part-19-](http://javathehutt.blogspot.com/2006/08/rails-realities-part-19-) relation.html

Also if you need to initialize a relationship when you create a new  
instance see this post from hasmanyjosh:

[http://blog.hasmanythrough.com/2007/1/22/using-faux-accessors-to-](http://blog.hasmanythrough.com/2007/1/22/using-faux-accessors-to-) initialize-values

Hope that helps,

-Michael [http://javathehutt.blogspot.com](http://javathehutt.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Rick8](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rick8](https://discuss.rubyonrails.org/u/Rick8)\
**Post date:** [February 16, 2007, 6:59pm UTC](https://discuss.rubyonrails.org/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-arrays-between-the-controller-and-view/8846/4 "2007-02-16T18:59:33Z")

</div>

Thanks, I will try your suggestions shortly.

I'm open to suggestons about my interface. If I wanted to make the inerface easy (for me) I would make the user create an Account, and then later go back and add assigned Employees. I think it will be more convenient (but not for me) to be able to assign employees on the same form that creates the account.

---

<div class="post-metadata">

**Author:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [February 16, 2007, 7:02pm UTC](https://discuss.rubyonrails.org/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-arrays-between-the-controller-and-view/8846/5 "2007-02-16T19:02:12Z")

</div>

Rick wrote:

> Thanks, I will try your suggestions shortly.

Try Mike's sooner. 😉

> I'm open to suggestons about my interface. If I wanted to make the inerface easy (for me) I would make the user create an Account, and then later go back and add assigned Employees. I think it will be more convenient (but not for me) to be able to assign employees on the same form that creates the account.

The best deal there is show them a working interface as soon as possible, with a few important features in it. Then they can request a better look-and-feel, based on experience with the prototype...

---

<div class="post-metadata">

**Author:** ![Rick8](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rick8](https://discuss.rubyonrails.org/u/Rick8)\
**Post date:** [February 16, 2007, 8:15pm UTC](https://discuss.rubyonrails.org/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-arrays-between-the-controller-and-view/8846/6 "2007-02-16T20:15:30Z")

</div>

I'm quite lost.

I tried the suggestions at the first link...the check\_box\_tag, which comes from your page, seems to work.

\<% @account.account\_employee\_relations.each do |child| %\> \<%=check\_box\_tag 'account[account\_employee\_relation\_ids]', child.id, true %\> \<br/\> \<%end%\>

This produces (for each check box):

\<input checked="checked" id="account[account\_employee\_relation\_ids]" name="account[account\_employee\_relation\_ids]" type="checkbox" /\>

But when I change the check box to a text field:

\<%= text\_field 'account[account\_employee\_relation\_ids]', child.id %\>

I get an error:

`@account[account\_employee\_relation\_ids]' is not allowed as an instance variable name
