Pergunta

Whenever I've had to do a HABTM in rails I've always found myself wondering if it's possible to generate the required migration from the command line.

I was hoping to save time by just doing something like this:

rails g migration tracks_podcasts tracks:references podcasts:references id:false

The above code doesn't work, and neither do several variations of it (with and without the id:false instruction)

Foi útil?

Solução

According to the rails documentation, that should work, but you would want to replace 'references' with 'integer'. 'references' isn't a valid data type. And get rid of the id:false entry. The references and :id => false should be specified within the migration after generation. You will basically generate a basic migration with all the fields you want, then open the migration and modify it to meet your specs.

Usage:
  rails generate migration NAME [field:type field:type] [options]

Options:
  -o, --orm=NAME  # Orm to be invoked
                  # Default: active_record

Runtime options:
  -f, [--force]    # Overwrite files that already exist
  -p, [--pretend]  # Run but do not make any changes
  -q, [--quiet]    # Supress status output
  -s, [--skip]     # Skip files that already exist

Description:
    Create rails files for migration generator.

Outras dicas

https://github.com/zealot128/ruby-habtm-generator is a nice option for this now.

Example:

rails g habtm user post
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top