Accessing a model from a migration

Hello again!

ok, i have some initial migration code which sets up my apps schema. i
have a
few tables that get initialized with data in this migration where i have
the
table data in yaml format in test/fixtures. i use db:fixtures:load
FIXTURES=countries,provstates,employees to initialize these tables…

here’s the wrinkle: employees when it gets loaded should only contain
the admin
account (so i can access the app after running the migration)… however
employees.yml also contains some other accounts of various priveleges
for
testing purposes. what i want to do is load the fixture into the db, but
delete
all the records except for the admin account… if i do:

cmd = ‘rake db:fixtures:load FIXTURES=countries,provstates,employees’
system cmd
Employee.delete_all “username != ‘admin’”

I get an error on the line that uses the Employee model. i tried a few
diff
variations of require’ing the model, but haven’t had any luck…

my short term solution was to just have the admin account in the
fixture, but
now when i want to test i don’t have those other accounts to test with…

i’m pretty sure i could use ActiveRecord::Base to do this with a little
bit of
digging, but if i could use the model directly it would be much
simpler…

thanks a bunch !

stuart

google search “rails using models in migrations”

1st hit: http://rails.techno-weenie.net/tip/2006/2/23/
safely_using_models_in_migrations

Not trying to be snarky Stuart, it’s just that we don’t always
realize the power of the tools we have at hand. It’s sometimes easier
to ask - but the community benefits as a whole when we’re all more
powerful information users. (something about “teaching to fish”)

cheers,
Jodi

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs