# I'm about to give up...I guess noone can help with passing a

**URL:** <https://www.ruby-forum.com/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-a/85848>\
**Category:** Rails\
**Created:** [February 16, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-a/85848 "2007-02-16T19:24:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 16, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-a/85848/1 "2007-02-16T19:24:44Z")

</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:

def new  
@account = Account.new  
@relation = AccountEmployeeRelation.new  
end

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

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

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

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

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

But I’m stuck on the view.

---

<div class="post-metadata">

**Author:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 16, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-a/85848/2 "2007-02-16T19:41:46Z")

</div>

Rick S. wrote:

> def new  
> @account = Account.new  
> @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.

```
 3.times do |idx|
   instance_eval "@relations_#{idx} = AccountEmployeeRelation.new"
 end

```

…

> ```
> @account = Account.new(params[:account])
> 
> ```

```
 3.times do |idx|
    relation = 

```

AccountEmployeeRelation.new(params[:“relation\_#{idx}”])  
account.account\_employee\_relations \<\< relation  
end  
…

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

```
 3.times do |idx| # similar symbol-abuse here...

```

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

> Employee: \<%= f.select (:employee\_id, Employee.find(:all).map{|u|  
> [u.name, u.id]}) %\>  
>   
> \<% 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.

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 16, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-a/85848/3 "2007-02-16T19:58:49Z")

</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:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 16, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-a/85848/4 "2007-02-16T20:02:49Z")

</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…

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 16, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-a/85848/5 "2007-02-16T21:16:08Z")

</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 %\>   
  
\<%end%\>

This produces (for each check box):

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

---

<div class="post-metadata">

**Author:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 16, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-a/85848/6 "2007-02-16T20:00:49Z")

</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:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 17, 2007, 2:28am UTC](https://www.ruby-forum.com/t/im-about-to-give-up-i-guess-noone-can-help-with-passing-a/85848/7 "2007-02-17T02:28:42Z")

</div>

Rick,

I did something this with a bit of AJAX. Basically, I gave the user the  
option to tell me how many fields they wanted, and then had the form  
load a partial with teh right number of fields.

Here’s the relevant bit of the \_form.rhtml partial:

p\>Select  
options

  
number of options: \<%= text\_field ‘select\_field’, ‘options\_number’,  
:id=\>“watch\_this”, :value=\>@num%\>
 \<%= render :partial=\>"select\_fields", :locals=\>{:num=\>@num}%\>
\<%= observe\_field "watch\_this", :url=\>{:action=\>"select\_fields", :controller=\>"custom\_fields",:params=\>{:campaign\_id=\>@campaign.id, :id=\>@custom\_field.id}}, :update=\>"select\_fields", :with=\>"num" %\>

And the \_select\_fields partial:

\<% if num.nil?  
num = @num  
end  
%\>  
\<% if num.nil? or num == 0  
num=3  
end%\>  
enter “select” options:  
  
\<% num.times do |i|  
if !@custom\_field.nil? and @custom\_field.field\_type==“Select”  
value = @custom\_field.select\_options[i]  
else  
value = “”  
end -%\>  
\<%=(i+1).to\_s%\>) \<%= text\_field ‘select\_field’, ‘options’,:index=\>i,  
:value=\>value %\>  
  
\<% end -%\>

The select\_fields action just collects and passes through the number of  
fields requested:

def select\_fields  
unless params[:id].nil?  
@custom\_field = CustomField.find(params[:id])  
end  
@num = params[:num].to\_i  
render :partial=\>‘select\_fields’, :protocol=\>@@protocol  
end

Hope that’s helpful. You could do it with just an RJS response to a  
form\_remote\_tag instead of watching the field if you want.

In the end the (variable number of) params come in as a nested hash,  
which I then pull apart and assign appropriately.

Asa
