# Retrieve value from text\_field and save it in another model

**URL:** <https://www.ruby-forum.com/t/retrieve-value-from-text-field-and-save-it-in-another-model/161422>\
**Category:** Rails\
**Created:** [February 26, 2009, 11:04am UTC](https://www.ruby-forum.com/t/retrieve-value-from-text-field-and-save-it-in-another-model/161422 "2009-02-26T11:04:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmswarna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rmswarna](https://www.ruby-forum.com/u/rmswarna)\
**Post date:** [February 26, 2009, 11:04am UTC](https://www.ruby-forum.com/t/retrieve-value-from-text-field-and-save-it-in-another-model/161422/1 "2009-02-26T11:04:21Z")

</div>

HI,

```
  I am very new to rails.

```

Any help from your side will be very helpful.

I have two models Child.rb and Filtersetting.rb I have written the code  
for inserting into both models with many rows at the same time. It works  
fine. The issue is that I need the childname entered correspondingly by  
the user to be transferred to the Filtersetting table.

How could i do this. Any ideas from your side in this regard will be  
very supportive. I have spent around full two days on this.But no  
progress.

Please could anyone help me in this regard.

The code is as follows.

class ChildrenController \< ApplicationController

layout ‘standard’

def index  
parent\_id = session[:user].id  
@children = Child.find(:all, :conditions =\> [“parent\_id = ?”,  
parent\_id])  
if @children.empty?  
flash[:notice] = “No child set”  
end  
respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @children }  
end  
end

def new  
@children = Array.new(3) { Child.new }  
@filters = Array.new(3) { Filtersetting.new }  
end

def create  
@children = params[:children].values.collect { |child|  
Child.new(child) }  
@filters = params[:filters].values.collect { |filter|  
Filtersetting.new(filter) }  
if @filters.all?(&:valid?)

```
 @filters.each(&:save!)

```

end  
if @children.all?(&:valid?)  
@children.each(&:save!)  
redirect\_to :action =\> ‘index’  
else  
render :action =\> ‘new’  
end

end

end

The view is

# Apply Children Settings

\<%= start\_form\_tag :action =\> ‘create’ %\>

\<% @children.each\_with\_index do |child, index| %\> 

| \<% fields\_for "children[#{index}]", child do |f| %\> Childname: \<%= f.text\_field :childname %\> \<%= f.hidden\_field :parent\_id, :value =\> current\_user.id %\>

```
   <% end %></td></tr>

```

\<% end %\>

 |

\<% @filters.each\_with\_index do |filter, index| %\> 

| \<% fields\_for "filters[#{index}]", filter do |f| %\> Filtering Level: \<%= f.collection\_select(:levelid, Level.find(:all), :id, :levelname) %\>

```
  <% end %></td></tr>

```

\<% end %\>

 |

\<%= submit\_tag "Submit" %\>

\<% end\_form\_tag %\>

\<%= link\_to ‘Back’, children\_path %\>

The issue is I want to store the childname and childid in Filtersetting  
table. That is the first childname and childid corresponding to first  
one in Filtersetting table.

I tried out with array and observe field but in vain. Kindly help me in  
this regard please.

Regards,  
Swarna

---

<div class="post-metadata">

**Author:** ![rmswarna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rmswarna](https://www.ruby-forum.com/u/rmswarna)\
**Post date:** [February 26, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/retrieve-value-from-text-field-and-save-it-in-another-model/161422/2 "2009-02-26T21:07:18Z")

</div>

I’m not quite sure what you’re after, but my guess is you want to  
associate children and filtersetting objects. Have you set up an  
association in the model files?

On Thu, Feb 26, 2009 at 2:04 AM, Swarna Priya  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have two models Child.rb and Filtersetting.rb I have written the code  
> for inserting into both models with many rows at the same time. It works  
> fine. The issue is that I need the childname entered correspondingly by  
> the user to be transferred to the Filtersetting table.  
> […]
