# Problem adding data to db

**URL:** <https://www.ruby-forum.com/t/problem-adding-data-to-db/105334>\
**Category:** Rails\
**Created:** [May 28, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/problem-adding-data-to-db/105334 "2007-05-28T23:01:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [May 28, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/problem-adding-data-to-db/105334/1 "2007-05-28T23:01:33Z")

</div>

Hello i am trying to save some data to my db, this is what i got:

MODEL:  
class File \< ActiveRecord::Base  
belongs\_to :product

def uploaded\_file=(picture\_field)  
self.name = base\_part\_of(picture\_field.original\_filename)  
self.content\_type = picture\_field.content\_type.chomp  
self.data = picture\_field.read  
end

def base\_part\_of(file\_name)  
File.basename(file\_name).gsub(/[^\w.\_-]/, ‘’)  
end  
end

CONTROLLER:

def create  
@product = Product.new(params[:product])  
@product.save

```
for file in params[:file]
  @file = File.new(params[:file])
  @file.producto_id = @producto.id
  @file.save
end

if @product.save
  flash[:notice] = 'Product created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

```

end

VIEW:  
i render a partial, and tis is the partial:  
\<%= error\_messages\_for(:product)%\>

Name  
\<%= text\_field 'product', 'name' %\>

Description  
\<%= text\_area 'product', 'description' %\>

 Image \<% fields\_for "file[]" do |f|%\> 

File:   
 \<%= f.file\_field :uploaded\_file %\>

 \<%end%\> Pdf \<% fields\_for "file[]" do |f|%\> 

File:   
 \<%= f.file\_field :uploaded\_file %\>

 \<%end%\>

When i try to save i got this error:

undefined method `keys’ for #Array:0x482bbf4

RAILS\_ROOT: ./script/…/config/…  
Application Trace | Framework Trace | Full Trace

#{RAILS\_ROOT}/app/controllers/admin/product\_controller.rb:30:in `create' -e:4:in`load’  
-e:4  
Parameters: {“product”=\>{“description”=\>“asdfasdfasd”,  
“name”=\>“aasfasdf”},  
“file”=\>[{“uploaded\_file”=\>#\<File:C:/Windows/Temp/CGI.5992.3\>},  
{“uploaded\_file”=\>#\<File:C:/Windows/Temp/CGI.5992.4\>}],  
“commit”=\>“Create”}  
Response  
Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

helps plis :P, regards

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [May 29, 2007, 1:00am UTC](https://www.ruby-forum.com/t/problem-adding-data-to-db/105334/2 "2007-05-29T01:00:12Z")

</div>

I have tried too, this solution but it get fail when try to save the  
second object.

def create  
@product = Product.new(params[:product])  
@product.save

```
  @file = Archivo.new(params[:file][0])
  @file2 = Archivo.new(params[:file][1])
  @file.product_id = @product.id
  @file2.product_id = @product.id
  @file.save
  @file2.save

if @product.save
 flash[:notice] = 'Product created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

```

end

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [May 29, 2007, 1:09am UTC](https://www.ruby-forum.com/t/problem-adding-data-to-db/105334/3 "2007-05-29T01:09:31Z")

</div>

wow LOL, sorry the relly problem was on the file i was trying to upload  
:P,

i kept with the first way, is better
