# Arrays to database

**URL:** <https://www.ruby-forum.com/t/arrays-to-database/182273>\
**Category:** Rails\
**Created:** [January 25, 2010, 9:58pm UTC](https://www.ruby-forum.com/t/arrays-to-database/182273 "2010-01-25T21:58:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [January 25, 2010, 9:58pm UTC](https://www.ruby-forum.com/t/arrays-to-database/182273/1 "2010-01-25T21:58:17Z")

</div>

Hello I’m a RoR newbe facing a problem.

I would like to upload two arrays to a database called ‘wrtlist’ i  
used this migration file to create the database

class AddWrtsList \< ActiveRecord::Migration  
def self.up  
create\_table :wrtlist do |t|  
t.string :title  
t.string :link  
t.string :publisher

```
  t.timestamps
end

```

end

def self.down  
drop\_table :wrtlist  
end  
end

The first array contains the titles and the second contains the links,  
the first title correspondents to the first title and the second to  
the second…:  
@titles, @links

I also want to add the static content “publisher\_name” to publisher.

What should be the best way to create a loop to upload the content to  
the database?

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [January 26, 2010, 2:07am UTC](https://www.ruby-forum.com/t/arrays-to-database/182273/2 "2010-01-26T02:07:40Z")

</div>

Got this from the ruby forum. Make a hash out of the two.

key = [“1”,“2”,“3”]  
value = [“a”,“b”,“c”]  
Hash[\*key.zip(value).flatten]  
=\> {“1”=\>“a”, “2”=\>“b”, “3”=\>nil}

then you can do:

Hash.each do |key,val|  
Wrtlist.create(:title =\> key, :link =\> val)  
end

---

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [January 26, 2010, 2:18am UTC](https://www.ruby-forum.com/t/arrays-to-database/182273/3 "2010-01-26T02:18:12Z")

</div>

Of course, you don’t actually _need_ the Hash

titles = [“1”,“2”,“3”]  
links = [“a”,“b”,“c”]  
titles.zip(links).each do |title, link|  
Wrtlist.create(:title =\> title, :link =\> link,  
:publisher =\> ‘publisher\_name’)  
end

-Rob

On Jan 25, 2010, at 8:06 PM, Me wrote:

> Wrtlist.create(:title =\> key, :link =\> val)
> 
> > def self.up  
> > drop\_table :wrtlist
> 
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid  
> .  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en)  
> .

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [January 26, 2010, 4:40am UTC](https://www.ruby-forum.com/t/arrays-to-database/182273/4 "2010-01-26T04:40:26Z")

</div>

Ya but hashes are cool, man.

On Jan 25, 7:16 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
