# Newbie question---

**URL:** <https://www.ruby-forum.com/t/newbie-question/100239>\
**Category:** Rails\
**Created:** [July 27, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/newbie-question/100239 "2006-07-27T22:51:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 27, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/newbie-question/100239/1 "2006-07-27T22:51:43Z")

</div>

I wrote a ruby script that parses a file into key-values like so:  
class Parser

table = { }  
IO.foreach(‘Localizable.strings’) { |line|  
if line =~ /^ \s\* " (._?) " \s_ = \s\* " (.\*?) "/x  
table[$1] = $2  
end  
}  
end

I want to store the key-values in different colums on my database so  
that when I enter a key, it gives me the corresponding value…how  
should I go about doing this? Thanks for your help.  
ric

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 27, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/newbie-question/100239/2 "2006-07-27T23:11:02Z")

</div>

Hi Richard,

you might find  
[http://wiki.rubyonrails.org/rails/pages/HowToUseActiveRecordOutsideRails](http://wiki.rubyonrails.org/rails/pages/HowToUseActiveRecordOutsideRails)  
an  
interesting read.

I’ve done something like you did simply by requiring my activerecord  
models  
in my script and then for example do something like

table.each do |key, value|  
MyKeyValueModel.create(:key =\> key, :value =\> value)  
end

This of course isn’t as performant as using a method that uses the batch  
capabilities of your rdbms but for key-value files that don’t contain  
millions of pairs it might fit your needs.

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 28, 2006, 1:40am UTC](https://www.ruby-forum.com/t/newbie-question/100239/3 "2006-07-28T01:40:14Z")

</div>

Jan P. wrote:

> Hi Richard,
> 
> you might find  
> [http://wiki.rubyonrails.org/rails/pages/HowToUseActiveRecordOutsideRails](http://wiki.rubyonrails.org/rails/pages/HowToUseActiveRecordOutsideRails)  
> an  
> interesting read.
> 
> I’ve done something like you did simply by requiring my activerecord  
> models  
> in my script and then for example do something like
> 
> table.each do |key, value|  
> MyKeyValueModel.create(:key =\> key, :value =\> value)  
> end
> 
> This of course isn’t as performant as using a method that uses the batch  
> capabilities of your rdbms but for key-value files that don’t contain  
> millions of pairs it might fit your needs.
> 
> Cheers,  
> Jan

Thanks Jan,

One of my goals is to use a method with batch capabilities…do you know  
any resources that I might find useful?

thanks

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 28, 2006, 8:22am UTC](https://www.ruby-forum.com/t/newbie-question/100239/4 "2006-07-28T08:22:43Z")

</div>

Hi Richard,

ok if performance is an issue for you, you might have a look at  
[http://thread.gmane.org/gmane.comp.lang.ruby.rails/14131/focus=14216](http://thread.gmane.org/gmane.comp.lang.ruby.rails/14131/focus=14216)

Essentialy you might  
a. rely on ActiveRecord and use transactions to bundle your inserts and  
commit them after a while  
b. Create mysqldump like extended inserts and perform them through a  
direct  
connection to your database if you are on mysql

Regardless what you’re doing: Make sure to do some benchmarks and tweak  
things like my.cnf. Switching auto commit makes a great deal especially  
on  
innodb. If you are no expert in the administration of databases you  
might  
have a hard time in tweaking performance until it fits your needs. At  
least  
I had for an issue like yours quite some time ago. But I haven’t got  
numbers  
or wiki-entries from this task anymore…

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 28, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/newbie-question/100239/5 "2006-07-28T21:08:46Z")

</div>

Hi, Richard,

no problem. If you got some time we’ll rule things out.

First thing: What is your development.log telling you. Are things  
working so  
far? You might post the output…  
Second:  
Maybe its just a typo but if you copy&pasted the code then there are at  
least three things:

- it’s got to be table.each do
- create is IMHO a class method so you should write StringRecord.create
- in your each loop you extract key and value as variables from the  
table  
hash. So it’s not $1 and $2 but key and value. The whole line:  
StringRecord.create(:key =\> key, :value =\> value)

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 28, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/newbie-question/100239/6 "2006-07-28T17:42:28Z")

</div>

Jan P. wrote:

> Hi Richard,
> 
> ok if performance is an issue for you, you might have a look at  
> [http://thread.gmane.org/gmane.comp.lang.ruby.rails/14131/focus=14216](http://thread.gmane.org/gmane.comp.lang.ruby.rails/14131/focus=14216)
> 
> Essentialy you might  
> a. rely on ActiveRecord and use transactions to bundle your inserts and  
> commit them after a while  
> b. Create mysqldump like extended inserts and perform them through a  
> direct  
> connection to your database if you are on mysql
> 
> Regardless what you’re doing: Make sure to do some benchmarks and tweak  
> things like my.cnf. Switching auto commit makes a great deal especially  
> on  
> innodb. If you are no expert in the administration of databases you  
> might  
> have a hard time in tweaking performance until it fits your needs. At  
> least  
> I had for an issue like yours quite some time ago. But I haven’t got  
> numbers  
> or wiki-entries from this task anymore…
> 
> Cheers,  
> Jan

Thanks Jan,

I tried what you told me and it still didn’t work. I’m not sure if i’m  
doing something wrong (most likely since I’m new with ruby on rails and  
I seem to be quite confuse). this is what I wrote down below. I asked  
someone else too about it but they mentioned I could simply put my ruby  
script on the model lib. This is what i did. I was hoping if you had a  
chance you could guide me in the right direction. thank you so much  
jan!!!

my view:

# Importing Strings

# \<%=error\_messages\_for("string\_record")%\> \<%= start\_form\_tag({:action =\> 'save'}, :multipart =\> true)%\>

**Lenguage:**  
 \<%= select("variable", nil, @array\_of\_lenguages) %\>

**Path:**   
 \<%= file\_field ("string\_record", "import\_strings" )%\>

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

\<%= end\_form\_tag%\>

my save method in my controller:  
def save  
@string\_record = StringRecord.new(params[:string\_record])  
if @string\_record.save  
redirect\_to(:action=\>‘show’, :id =\>@string\_record.id)  
else  
render(:action=\>:get)  
end  
end

this is my active record class:  
class StringRecord \< ActiveRecord::Base

def import\_strings  
table = { }  
IO.foreach(‘Localizable.strings’) { |line|  
if line =~ /^ \s\* " (._?) " \s_ = \s\* " (.\*?) "/x  
table[$1] = $2  
end

```
table.eabh do |key, value|
string_record.create(:key => $1, :value => $2)
end
}

```

end  
end

rich

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 29, 2006, 12:07am UTC](https://www.ruby-forum.com/t/newbie-question/100239/7 "2006-07-29T00:07:19Z")

</div>

Jan P. wrote:

> Hi, Richard,
> 
> no problem. If you got some time we’ll rule things out.
> 
> First thing: What is your development.log telling you. Are things  
> working so  
> far? You might post the output…  
> Second:  
> Maybe its just a typo but if you copy&pasted the code then there are at  
> least three things:
> 
> - it’s got to be table.each do
> - create is IMHO a class method so you should write StringRecord.create
> - in your each loop you extract key and value as variables from the  
> table  
> hash. So it’s not $1 and $2 but key and value. The whole line:  
> StringRecord.create(:key =\> key, :value =\> value)
> 
> Cheers,  
> Jan

Hi Jan,

I fixed the typos and I get this error from my browser:  
Mysql::Error: #23000Column ‘date\_imported’ cannot be null: INSERT INTO  
string\_records (`platform`, `date_imported`, `context_of_string`,  
`identifier`, `lenguage`, `string`) VALUES(’’, NULL, ‘’, 0, ‘’, ‘’)

I think this is becasue I’m only uploading into two fields but my table  
has seven fields…nonetheless do you think I can upload only to two  
columns? Or do I need to create special table with only two fields?

thanks again

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 29, 2006, 12:43am UTC](https://www.ruby-forum.com/t/newbie-question/100239/8 "2006-07-29T00:43:32Z")

</div>

Hi Mike,

What do you want to model with the date\_imported column? If it is a  
import-date that you already have in your key-value file, then why don’t  
you  
set it? If you just want to have a timestamp on your table when  
something  
has been imported and updated then you might want to use the rails  
convention and use datetime fields named ‘created\_at’ and ‘updated\_at’,  
then  
rails will make the hard work and fill in the appropriate values for  
you.

Finally: If date\_imported should only on a few values be NULL you might  
want  
to change your schema. Seems as right now date\_imported is stated as  
‘NOT  
NULL’, change that to DEFAULT NULL and ActiveRecord / MySQL shouldn’t  
complain about the NULL value. You’ll find the exact syntax on the  
documentation of [mysql.com](http://mysql.com)

Cheers,  
Jan
