Command line generation of associations

All,

I’m new to Rails, but have used similar frameworks. I’m trying to find
out if it’s possible to specify associations from the command line
when creating models . So far, I’ve only seen an example of specifying
“references” as follows, which places a “belongs_to” association on
the model:

rails generate model Comment commenter:string body:text
post:references

Background
My group uses Enterprise Architect (EA) to design the logical models
for our applications. This allows us to focus closely on our domain
before ever working on code. Once we’re happy with the domain model,
we use custom tools to generate applications, thus avoiding lots of
menial coding.

I’ve been extending our tool to convert the EA XMI logical model into
scripts that can generate applications for Grails, Spring Roo and RoR.
So far, Roo has the best command line support for associations and
I’ve created my own extension for Grails to support this. Now, I’m now
working on RoR.

I would appreciate any pointers as to how I can extend the current
ActiveRecord / Rails code to add the ability to define the standard
‘has_one’, ‘has_many’ and ‘has_and_belongs_to_many’. Due to my
inexperience with RoR I’m hitting a roadblock when attempting to find
where the “references” is handled by the model generator.

Many thanks,
Bill S.

On Oct 24, 2011, at 5:59 AM, wistephens wrote:

I’ve created my own extension for Grails to support this. Now, I’m now
working on RoR.

I would appreciate any pointers as to how I can extend the current
ActiveRecord / Rails code to add the ability to define the standard
‘has_one’, ‘has_many’ and ‘has_and_belongs_to_many’. Due to my
inexperience with RoR I’m hitting a roadblock when attempting to find
where the “references” is handled by the model generator.

The references bit on the command-line only affects the migration. It’s
just a shortcut for [other_model]_id. You still have to write the
appropriate relationship macros yourself, since only you know what that
precise relationship is (has_one, has_many, has_and_belongs_to_many,
belongs_to, etc…). You’d need to have some way to take your logical
model and convert it to the Rails idiom, and the only way I can think of
for that to work seamlessly is if you read the Rails Guide on
relationships, and then create a mapping between your logical
relationships and the standard Rails relationships. Then you’ll probably
also want your converter to kick out an error when it finds a
relationship that is either ambiguous or doesn’t fit one of the basic
relationship types. There are always ways around those edge cases, but
it’s not trivial to think of all of them beforehand.

Walter