This project is archived and is in readonly mode.

#1315 ✓wontfix
CancelProfileIsBroken

Extend model generator to create association declarations

Reported by CancelProfileIsBroken | November 2nd, 2008 @ 05:51 PM | in 2.x

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

Comments and changes to this ticket

  • Ed Spencer

    Ed Spencer November 3rd, 2008 @ 09:12 PM

    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)

  • Pratik

    Pratik December 21st, 2008 @ 03:08 PM

    • State changed from “new” to “wontfix”

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

  • bingbing

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages