Ruby Forum Rails Engines development > Help with a noob

Posted by Todd Nine (Guest)
on 10.02.2008 19:57
(Received via mailing list)
Hi all,
  I'm a noob to creating my own plugins, and I could use a hand.  I've 
only
developed basic rails apps, and several advanced J2EE apps.  I 
understand
how my plugin structure mirrors the rails app mvc in the base app, but 
I'm
having trouble with creating my models.  I created a new plugin via
"script/generate plugin projects_ranking".  I'd like to create the model 
for
my database migration, but I don't know the command for generating a 
model
within a plugin?  Where can I find documentation on generating models 
within
a plugin?

thanks,
Todd
Posted by Rob Morris (Guest)
on 11.02.2008 02:37
(Received via mailing list)
Hey Todd, welcome to Rails plugin development.  :-)

Basically, a model is just a class derived from ActiveRecord::Base, so,
the class file itself is just

class FooModel < ActiveRecord::Base

end

I don't bother with script/generate for models, for this reason.  It's
too simple.

The hard part is then building the migration to set up the database
table foo_models.  I'm assuming you're familiar with migrations, so the
only tricky part is how you add them from a plugin.

Go to your plugin's root directory, and add a directory 'db', then a
subdir of that named 'migrate'.  Add your migration(s) there.

Once you're happy with your initial migration, go to your Rails root
dir, and run: ./script/generate plugin_migration

That will add a new migration in your main rails app (basically a
wrapper) for upgrading to add your plugin migration(s).  You can check
out what is generated by going to RAILS_ROOT/db/migrate, and editing the
newly created file.  It's just a proxy call that asks the plugin to
migrate to a particular version.

Then rake db:migrate from your rails root like normal, and your new
schema is in place, and your new model will pull from it, and be
available throughout your app.

Cheers!

-Rob