Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Extend model generator to create association declarations

#1315

In Rails 2.2, if you include a belongs_to or references column in a call to the model generator, it will automatically add the belongs_to declaration to the model, as well as creating the foreign key column in the migration.

This patch extends the generator to pass has_many, has_one, and has_and_belongs_to_many "pseudo-attributes" through to the model, creating the appropriate association declarations without adding columns to the migrations or fixtures.

Sample usage:

script/generate model Supplier name:string products:has_many

script/generate model User name:string account:has_one

script/generate model Product name:string parts:has_and_belongs_to_many

Reported by CancelProfileIsBroken · November 2nd, 2008 @ 05:51 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Ed Spencer
    Ed Spencer

    This is a good idea, and something I have done with my own extensions in the past. Getting the syntax right is tricky though, as some things could be chained, e.g.:

    script/generate model Suppler name:string:vp:vu products:has_many

    Or

    script/generate model Suppler name:string:validates_presence:validates_uniqueness products:has_many

    Could each generate:

    class Supplier < ActiveRecord::Base validates_presence_of :name validates_uniqueness_of :name has_many :products end

    The first syntax is quite opaque, and the second almost as verbose as the actual declarations in the class. I prefer the first... but there might be a better way.

    This of course becomes more powerful when the unit tests are generated also, correctly testing the additional behaviour (slightly OT but the type of syntax in http://github.com/edspencer/rspe... would lend itself well to this situation)

    November 3rd, 2008 @ 09:12 PM

  2. Pratik
    Pratik
    • State changed from new to wontfix

    Not sure if this is a right fit or not. Let's have a plugin first.

    December 21st, 2008 @ 03:08 PM

  3. bingbing