Rails 2 foxy fixtures and named_scope/has_finder closure issues
has_finder :all_active, { :conditions => {:status_id => Status.active_ids} }
This work in production, where the ids don’t change, but since we are using the newer fixture approach on this model the ids are created dynamically. Given how fixtures load, the table will probably be populated with the old values, at parse time for that class, then the test will run, and the fixtures will reload, and they will not match anymore.
This is easily fixed, when you realize what is happening:
has_finder :all_active, lambda {|| { :conditions => {:status_id => OtherModel.active_ids} }}
Backing up or transfering your data via YAML 4
Thanks to http://snippets.dzone.com/tag/fixtures. No warranties and no guarantees it’ll work on your full DB. I’m just using it for transferring around my starting data during dev.
Yes, I know part of this can be done with db:fixtures:load – but I don’t want to use ‘tests/fixtures’
lib/tasks/fixtures.rake
namespace :db do
namespace :YAML do
desc 'Create YAML backup fixtures'
task :backup => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_info", "sessions"]
ActiveRecord::Base.establish_connection
tables = ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : ActiveRecord::Base.connection.tables - skip_tables
tables.each do |table_name|
i = "000"
File.open("#{RAILS_ROOT}/db/fixtures/#{table_name}.yml", 'w') do |file|
data = ActiveRecord::Base.connection.select_all(sql % table_name)
file.write data.inject({}) { |hash, record|
hash["#{table_name}_#{i.succ!}"] = record
hash
}.to_yaml
end
end
end
desc "Load db/fixtures in fixtures_load_order"
task :restore => :environment do
ActiveRecord::Base.establish_connection
require 'active_record/fixtures'
Fixtures.create_fixtures("db/fixtures", ActiveRecord::Base.configurations[:fixtures_load_order])
puts "Loaded these fixtures: " + ActiveRecord::Base.configurations[:fixtures_load_order].collect { |f| f.to_s }.join(', ')
end
end
end
config/environments.rb
ActiveRecord::Base.configurations[:fixtures_load_order] = [
:users,
:locations,
:reports,
:conditions
]
Testing plugins : loading fixtures and environment
require 'test/unit'
require 'active_record'
require 'active_record/fixtures'
RAILS_ENV = "test"
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
class TypoPermalinkWithIdTest < Test::Unit::TestCase
def setup
fixtures_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../../../test/fixtures'))
Fixtures.create_fixtures(fixtures_dir, File.basename("blogs.yml", '.*'))
Fixtures.create_fixtures(fixtures_dir, File.basename("users.yml", '.*'))
end
def test_fixtures_loaded
assert_not_nil Blog.find_by_id(1), "blogs.yml not loaded"
assert_not_nil User.find_by_id(1), "users.yml not loaded"
end
Edit: This idea slightly cribbed from Loading Fixtures in a Migration

Articles via rss or email