|How to create a seed script in ruby using DataMapper to populate db?||supamanu||3/2/12 11:03 AM|
I'm building a simple sinatra app using DataMapper as my ORM and pgsql
as both my dev and prod db (pushing to heroku).
This is all pretty new stuff to me. What I can't figure out is how to
build a seed script in ruby so that I can quickly re-populate my db
locally whenever I destroy it to make changes in my model/underlying
tables (i'm iterating a lot as I find my way around associations).
The thing is once my server is running and I'm on my web page, I don't
know where to place the seed file or how to call it. Can I just open a
terminal window and e.g. run a 'seed.rb' file? Will all the
dependencies already be loaded since my app is running?
I guess what i'm trying to say is that my mindset is 'everything goes
through a url with get/post etc' and I can't reconcile that with 'oh,
i need to run a script to fill some tables'. And I would rather keep
everything in ruby using DataMapper and not have to use psql commands
to do the seeding.
Thanks for you help.
|Re: How to create a seed script in ruby using DataMapper to populate db?||Mike||3/2/12 11:21 AM|
1.) write a class that does the seeding with a seed method. e.g. class DbSeeds; def self.seed; DO IT; end; end;
2.) run irb from your project root
3.) require your 'root' file. e.g. MyApp.rb (i'm assuming this app is requiring all your models and everything. If it isn't, require the file that does)
4.) run the method that does the seeding in the seeds class. e.g. DbSeeds.seed (assuming seeds deletes the existing data first, if not…do that first)
5.) there is no 5
|Re: How to create a seed script in ruby using DataMapper to populate db?||supamanu||3/2/12 2:02 PM|
Mike, you rock! Thanks a bunch.
|Re: How to create a seed script in ruby using DataMapper to populate db?||Mike||3/2/12 2:17 PM|
Glad to help!
|Re: How to create a seed script in ruby using DataMapper to populate db?||ericgj||3/2/12 3:59 PM|
Besides IRB, there's also tux which is like the Rails console but even