Rails template - adding index to :created_at using generate
ruby-on-rails, templates
Solution
I think you can simply pass additional arguments to generate, example: `"created_at:datetime:index"` and `"--no-timestamps"`
$ rails g model post content:string user_id:integer:index updated_at:datetime created_at:datetime:index --no-timestamps
invoke active_record
create db/migrate/20140712155207_create_posts.rb
create app/models/post.rb
invoke test_unit
create test/models/post_test.rb
create test/fixtures/posts.yml
$ cat db/migrate/20140712155207_create_posts.rb
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :content
t.integer :user_id
t.datetime :updated_at
t.datetime :created_at
end
add_index :posts, :user_id
add_index :posts, :created_at
end
end
Alternatively, you could always generate a migration after the model generation:
$ rails generate migration AddIndexToPosts created_at:datetime:index
Obviously, you would need to make this conform the `generate()` syntax.
Problem
I'm creating a Rails template. The template includes `generate(:model, "Post", "content:string", "user_id:integer:index")`, which works fine to generate a model for Post. However, I'd also like to add an index to the timestamp `:created_at` using the `generate` command for Rails templates. Is this possible within the previously mentioned generate model, or via another way?