gundestrup-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Oct-18 12:19 UTC
Import of YAML files during rake?
I want to import yaml files in to my project. I have tried with fixtures, but it deletes the current data of the table. I need a way to import yaml filen in to a exsistant table. regards svend --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 18, 11:19 pm, "gundest...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <gundest...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want to import yaml files in to my project. > > I have tried with fixtures, but it deletes the current data of the > table. > I need a way to import yaml filen in to a exsistant table. >If you want to load yaml data into your development database, for instance, you can add migrations to do this. In db/migrate you should have the migration files for your tables which you created when you generated your models. Let''s suppose you are loading to the ''users'' table then you might run: % script/generate migration LoadUserData Migration file might look like this: ---- require ''active_record/fixtures'' class LoadUserData < ActiveRecord::Migration def self.up down directory = File.join( File.dirname(__FILE__) , ''data'' ) Fixtures.create_fixtures(directory, ''users'') end def self.down User.delete_all end end ---- where; ''data'' is db/migrate/data directory; and ''users'' is the db/migrate/data/users.yml file - which will load into ''users'' table for your ''User'' model. Then run your migrations: % rake db:migrate VERSION=0 % rake db:migrate I think this technique is in the Rails book. -- Daniel Bush --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gundestrup-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Oct-18 19:13 UTC
Re: Import of YAML files during rake?
Thanx, but using fixtures_create removes the current contense of the database. This is not for devel, this is in the production enviroment. regards svend On Oct 18, 4:03 pm, Daniel <dlb.id...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 18, 11:19 pm, "gundest...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <gundest...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > I want to import yaml files in to my project. > > > I have tried with fixtures, but it deletes the current data of the > > table. > > I need a way to import yaml filen in to a exsistant table. > > If you want to load yaml data into your development database, for > instance, you can add migrations to do this. > In db/migrate you should have the migration files for your tables > which you created when you generated your models. Let''s suppose you > are loading to the ''users'' table then you might run: > % script/generate migration LoadUserData > > Migration file might look like this: > ---- > require ''active_record/fixtures'' > > class LoadUserData < ActiveRecord::Migration > def self.up > down > directory = File.join( File.dirname(__FILE__) , ''data'' ) > Fixtures.create_fixtures(directory, ''users'') > end > > def self.down > User.delete_all > end > end > ---- > where; > ''data'' is db/migrate/data directory; and > ''users'' is the db/migrate/data/users.yml file - which will load into > ''users'' table for your ''User'' model. > > Then run your migrations: > % rake db:migrate VERSION=0 > % rake db:migrate > > I think this technique is in the Rails book. > > -- > Daniel Bush--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 19, 6:13 am, "gundest...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <gundest...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanx, but using fixtures_create removes the current contense of the > database. > This is not for devel, this is in the production enviroment. > > regards > svend >I''ve only used the technique I mentioned to populate the table in its totality. If you''re appending data to a table, you could perhaps create a loading table as a copy of the table you''re loading to (sql: create <loading-table> as select * from <table> where 1=0; (not sure if it works on every db) / or do it the migration way). Use the fixture technique discussed to load it with data from your yaml file. Then run an insert sql statement: ''insert into <table> select * from <loading-table>''. -- Daniel Bush --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gundestrup-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Oct-19 16:41 UTC
Re: Import of YAML files during rake?
I hate it when people think out of the box, and comes up with a perfect simple solution. Perfect, thanx for the help. regards svend On Oct 19, 2:19 am, Daniel <dlb.id...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 19, 6:13 am, "gundest...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <gundest...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > Thanx, but using fixtures_create removes the current contense of the > > database. > > This is not for devel, this is in the production enviroment. > > > regards > > svend > > I''ve only used the technique I mentioned to populate the table in its > totality. If you''re appending data to a table, you could perhaps > create a loading table as a copy of the table you''re loading to (sql: > create <loading-table> as select * from <table> where 1=0; (not sure > if it works on every db) / or do it the migration way). Use the > fixture technique discussed to load it with data from your yaml file. > Then run an insert sql statement: ''insert into <table> select * from > <loading-table>''. > > -- > Daniel Bush--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---