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 -- Posted via http://www.ruby-forum.com/.
Hi Richard, you might find 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 On 7/27/06, Richard Smith <ricardo.orozco@hp.com> wrote:> > 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060727/00fab08e/attachment.html
Jan Prill wrote:> Hi Richard, > > you might find > 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, > JanThanks Jan, One of my goals is to use a method with batch capabilities...do you know any resources that I might find useful? thanks -- Posted via http://www.ruby-forum.com/.
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 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 On 7/28/06, Richard Smith <ricardo.orozco@hp.com> wrote:> > Jan Prill wrote: > > Hi Richard, > > > > you might find > > 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060728/230c4e5e/attachment-0001.html
Jan Prill 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 > > 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, > JanThanks 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: <h1>Importing Strings<h1> <%=error_messages_for("string_record")%> <%= start_form_tag({:action => ''save''}, :multipart => true)%> <p> <b>Lenguage:</b><br/> <%= select("variable", nil, @array_of_lenguages) %> </p> <p> <b>Path: </b><br /> <%= file_field ("string_record", "import_strings" )%> </p> <p><%= submit_tag "Import"%></p> <%= 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 -- Posted via http://www.ruby-forum.com/.
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 On 7/28/06, Richard Smith <ricardo.orozco@hp.com> wrote:> > Jan Prill 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 > > > > 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: > <h1>Importing Strings<h1> > <%=error_messages_for("string_record")%> > <%= start_form_tag({:action => ''save''}, :multipart => true)%> > <p> > <b>Lenguage:</b><br/> > <%= select("variable", nil, @array_of_lenguages) %> > </p> > <p> > <b>Path: </b><br /> > <%= file_field ("string_record", "import_strings" )%> > </p> > <p><%= submit_tag "Import"%></p> > > <%= 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060728/fb2c6b82/attachment-0001.html
Jan Prill 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, > JanHi 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 -- Posted via http://www.ruby-forum.com/.
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 Cheers, Jan On 7/29/06, Mike Williams <ricardo.orozco@hp.com> wrote:> > Jan Prill 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060728/211121c3/attachment.html