Skip to content

Add references statements to migration generator #6956

Merged
merged 2 commits into from Jul 8, 2012

5 participants

@lexmag
lexmag commented Jul 4, 2012

For instance, $ rails g migration AddReferencesToProduct user:references supplier:references{polymorphic} will generate:

add_reference :products, :user, index: true
add_reference :products, :supplier, polymorphic: true, index: true
@lexmag
lexmag commented Jul 4, 2012

How about along the way add singularized reference type definition in generators?
https://github.com/lexmag/rails/tree/reference_generators

@lexmag
lexmag commented Jul 6, 2012

@carlosantoniodasilva, please, review this request.

@rafaelfranca
Ruby on Rails member

Seems good.

@carlosantoniodasilva carlosantoniodasilva commented on an outdated diff Jul 7, 2012
...rators/active_record/migration/templates/migration.rb
@@ -2,30 +2,42 @@ class <%= migration_class_name %> < ActiveRecord::Migration
<%- if migration_action == 'add' -%>
def change
<% attributes.each do |attribute| -%>
+ <%- if attribute.reference? -%>
+ <%= migration_action %>_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
@carlosantoniodasilva
Ruby on Rails member

Why migration_action here, since the others just use add_*? I think you can just use add_reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@carlosantoniodasilva
Ruby on Rails member

Agreed, seems good to me as well. It just needs a changelog entry, some guide updates showing the new feature, and possibly an improved commit message with some example.

@lexmag could you take a look at these changes? Just ping us later. Thanks!

@lexmag
lexmag commented Jul 8, 2012

@rafaelfranca, @carlosantoniodasilva thanks.
All done. But i used old hash syntax in guide because it's everywhere in guide.

At the same time, I would like to ask your opinion about this topic https://groups.google.com/forum/#!topic/rubyonrails-core/aVMjIlf9zLk

@rafaelfranca
Ruby on Rails member

I think we can squash all three commits in one, since two are documentation changes.

I'll look at this topic too.

@lexmag
lexmag commented Jul 8, 2012

@rafaelfranca, what do you think about keep third commit separate because it adds subchapter "Supported type modifiers", which doesn't related with other commits?

@rafaelfranca
Ruby on Rails member

Seems fine.

lexmag added some commits Jul 8, 2012
@lexmag lexmag Add references statements to migration generator
AddXXXToYYY/RemoveXXXFromYYY migrations are produced with references
statements, for instance

    rails g migration AddReferencesToProducts user:references
supplier:references{polymorphic}

will generate the migration with:

    add_reference :products, :user, index: true
    add_reference :products, :supplier, polymorphic: true, index: true
0cae7c6
@lexmag lexmag Update migrations guide
Add a paragraph about references statements in migration generator
and the subchapter "Supported type modifiers"
07ebc53
@lexmag
lexmag commented Jul 8, 2012

Squashed.

@rafaelfranca rafaelfranca merged commit bbc08c7 into rails:master Jul 8, 2012
@cerdiogenes

I'm using Rails 4.0.2 and the command rails g migration AddReferencesToProduct user:references supplier:references{polymorphic} is generating the following:

add_reference :products, :user, index: true
add_column :products, :supplier, :referencespolymorphic

Can someone confirm this?

@matthewd
Ruby on Rails member
matthewd commented May 3, 2014

@cerdiogenes sounds like a shell quoting issue to me.

@cerdiogenes

You are right @matthewd. I'm using fish and when I tried the same command with bash it worked like a charm. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.