Its all about Ruby On Rails
Generating association while generating model
May be you guys are already aware of, but in Rails 2.2 we can specify belongs_to association while generating model. So if we issue:
script/generate model Post title:string author:belongs_to
We will get a Post model like:
class Post < ActiveRecord::Base belongs_to :author end
Also it will automatically add the foreign key column in the migration.
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.string :title
t.belongs_to :author
t.timestamps
end
end
def self.down
drop_table :posts
end
end
Nothing much but saves some keystrokes.
| Print article | This entry was posted by Akhil Bansal on December 14, 2008 at 4:40 pm, and is filed under Rails, Rubyonrails, Tips, active_record, associations, edge_rails, migration, model, tricks. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
Awesome. I hadn’t seen anyone write about this yet.