Quick question on fixtures. I know fixtures are primarily for unit testing but I was thinking of using them to help me with development. I need some realistic data so with the ERb templated fixtures it seems like I could generate that and insert it in my development database. That would give me real data to work with (instead of just a few records I have manually inserted in each table). But I have two questions on this: 1) Is there an easy way that I can say "load these fixtures into this database" or do I need to look at the Fixtures API and write a script do this (just wondering if there is already a rake task or something that will do this). 2) When it loads the fixture does it create a instance of that object then call save or do a straight insert. The reason I was wondering is because some fields are generated by ActiveRecord (updated_on, nested_set left and right columns, etc) and it seems that this data wouldn''t be generated. I would prefer not to have to type this stuff into the YAML file myself. Would it be better just to write a console script that uses ActiveRecord to create my test data? Eric _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I don''t have my code with me right now so I can''t share it now. But, basically what I did was create a test class in my db directory. That class inherits from UnitTest and uses all the fixtures I need. The class has one empty method This class "require" a special test_helper that set the environment property to development rather than test. Something like: require "special_helper" class < < Test::Unit::TestCase fixture :what_evers def test_load_fixtures end end Then I run that test. ------------------------ Not sure about #2 -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Eric Anderson Sent: Monday, November 07, 2005 7:18 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] Fixtures question Quick question on fixtures. I know fixtures are primarily for unit testing but I was thinking of using them to help me with development. I need some realistic data so with the ERb templated fixtures it seems like I could generate that and insert it in my development database. That would give me real data to work with (instead of just a few records I have manually inserted in each table). But I have two questions on this: 1) Is there an easy way that I can say "load these fixtures into this database" or do I need to look at the Fixtures API and write a script do this (just wondering if there is already a rake task or something that will do this). 2) When it loads the fixture does it create a instance of that object then call save or do a straight insert. The reason I was wondering is because some fields are generated by ActiveRecord (updated_on, nested_set left and right columns, etc) and it seems that this data wouldn''t be generated. I would prefer not to have to type this stuff into the YAML file myself. Would it be better just to write a console script that uses ActiveRecord to create my test data? Eric
eric wrote:> Quick question on fixtures. I know fixtures are primarily for unit > testing but I was thinking of using them to help me with development. I > need some realistic data so with the ERb templated fixtures it seems > like I could generate that and insert it in my development database. > That would give me real data to work with (instead of just a few records > I have manually inserted in each table). But I have two questions on > this: > > 1) Is there an easy way that I can say "load these fixtures into this > database" or do I need to look at the Fixtures API and write a script do > this (just wondering if there is already a rake task or something that > will do this).You can load your test fixtures in your development database. rake load_fixtures If you have constraints you need to add to the file ruby\lib\ruby\gems\1.8\gems\rails-1.0.0\lib\tasks\databases.rake: desc "Loads fixtures whilst turning foreign key constraints checking off" task :load_fixtures_without_constraints => :environment do require ''active_record/fixtures'' ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym) ActiveRecord::Base.connection.update "SET FOREIGN_KEY_CHECKS = 0" Dir.glob(File.join(RAILS_ROOT, ''test'', ''fixtures'', ''*.{yml,csv}'')).each do |fixture_file| Fixtures.create_fixtures(''test/fixtures'', File.basename(fixture_file, ''.*'')) end ActiveRecord::Base.connection.update "SET FOREIGN_KEY_CHECKS = 1" end I found this somewhere in the internet. Now you can load your fixtures mit rake load_fixtures_without_constraints. -- Posted via http://www.ruby-forum.com/.
eric wrote:> Quick question on fixtures. I know fixtures are primarily for unit > testing but I was thinking of using them to help me with development. I > need some realistic data so with the ERb templated fixtures it seems > like I could generate that and insert it in my development database.FYI, Chad Fowlers "Rails Recipes" has a few good bits and pieces on working with fixtures. Beta available now from Prags. Alan -- Posted via http://www.ruby-forum.com/.
At my work, we wanted to have different fixtures for development than for testing- this is how we did it. First, we created a directory called db/fixtures, and we added all the relevant fixture files. Next, we added a Rake task file called fixtures.rake, in the lib\tasks directory. That file contains (among other things): desc "Load fixtures data into the development database" task :load_development_fixtures => :environment do load_fixtures_helper :development, ''db/fixtures'' end def load_fixtures_helper(env, path) require ''active_record/fixtures'' ActiveRecord::Base.establish_connection(env) Fixtures.create_fixtures(path, ActiveRecord::Base.configurations[:fixtures_load_order]) end Then, to control the load order, I added this to config/environment.rb: ActiveRecord::Base.configurations[:fixtures_load_order] = [ :fixture_1, :fixture_2, :fixture_3 ] Now, I can call "rake load_development_fixtures" from the command line, and my development fixtures will be loaded into the development DB. Using a helper function let me create other tasks, such as load demo fixtures, or load minimal fixtures. On 4/3/06, Alan Francis <alancfrancis@gmail.com> wrote:> > eric wrote: > > Quick question on fixtures. I know fixtures are primarily for unit > > testing but I was thinking of using them to help me with development. I > > need some realistic data so with the ERb templated fixtures it seems > > like I could generate that and insert it in my development database. > > FYI, Chad Fowlers "Rails Recipes" has a few good bits and pieces on > working with fixtures. Beta available now from Prags. > > Alan > > -- > 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/20060403/05a39db6/attachment.html