Both of those are bad ideas in my opinion.
Fixtures should be used for test data only. Migrations seem like a logical
spot for defalt data, but they can become brittle if you try to place
records into the database at that time, as future changes to code
(validations, before_ callbacks, etc) can make them fail, killing your
remaining migrations. Even if it seems like you can get away with it, I
wouldn''t.
Here''s the way I do it... by making my own rake task.
In your lib/tasks folder, create a file called import.rake
Place this code in the file
namespace :db do
desc "import initial database contents"
task :import => :environment do
admin = User.create(:login => "admin", :password
=>"1234",
:password_confirmation=>"1234",
:email=>"admin-9IKiO1iGCm/QT0dZR+AlfA@public.gmane.org")
admin_role = Role.create :name => ''admin''
moderator_role = Role.create :name => ''moderator''
admin.roles << admin_role
admin.roles << moderator_role
end
end
Save it, then run
rake db:import
or
rake RAILS_ENV="production" db:import
And you''re all set. Make that part of your deploy:cold step in
capistrano :)
Good luck!
On Tue, Feb 26, 2008 at 10:24 AM, Jabbslad
<jabbslad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> Hi, yep thats fine if you think they are quite static and not likely
> to change. Or you could add them as fixtures and use "rake
> db:fixtures:load" to load them into your database.
>
> On Feb 26, 4:06 pm, Peter
<peterdun...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > What would be a good way to create some predefined instances in the
> > database for a certain model? For example: categories, genres or user-
> > roles? They are pretty fixed, although you might want to add or remove
> > some at a later stage.
> >
> > Is it a good idea to put something like this in the migration itself?
> >
> > def self.up
> > create_table :roles do |t|
> > ..
> > end
> >
> > Role.create :name => ''admin''
> > Role.create :name => ''moderator''
> > end
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---