Generating association while generating model

Published on Author Akhil Bansal1 Comment

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 [/ruby] Also it will automatically add the foreign key column in the migration. [ruby] 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 [/ruby] Nothing much but saves some keystrokes. 🙂

One Response to Generating association while generating model

Leave a Reply to akahn Cancel reply

Your email address will not be published. Required fields are marked *