What’s the correct syntax for remove_index in a Rails 3.1.0 migration?
For the record, the way to remove an index by name is remove_index(:table_name, :name => ‘index_name’) so in your case remove_index(:users, :name => ‘index_users_on_email’)
For the record, the way to remove an index by name is remove_index(:table_name, :name => ‘index_name’) so in your case remove_index(:users, :name => ‘index_users_on_email’)
You can run the code in the up method directly in rails console: >> ActiveRecord::Migration.remove_column :table_name, :column_name If you already have a migration file such as “db/migrate/20130418125100_remove_foo.rb“, you can do this: >> require “db/migrate/20130418125100_remove_foo.rb” >> RemoveFoo.up If you just want to do rake db:migrate, try this: >> ActiveRecord::Migrator.migrate “db/migrate”
As of Rails 4.1, the rake db:test:* tasks are deprecated. Instead, your (test|spec)_helper.rb should include: ActiveRecord::Migration.maintain_test_schema! This means that your test database will get the correct schema every time your tests run, whether you run them from a Rake task or not.
You could call: rails g model task user:references which will generates an user_id column in the tasks table and will modify the task.rb model to add a belongs_to :user relatonship. Please note, you must to put manually the has_many :tasks or has_one :task relationship to the user.rb model. If you already have the model generated, … Read more
If you’re talking about the types for migrations, e.g. string, integer, datetime, etc, then you want ActiveRecord::ConnectionAdapters::TableDefinition, the column method. (Rails 5 edit: see also connection.add_column.) As of this update, the standard types are: :primary_key :string :text :integer :bigint :float :decimal :numeric :datetime :time :date :binary :boolean The implementation of :decimal is different with each … Read more
Unfortunately, you must manually clean up failed migrations for MySQL. MySQL does not support transactional database definition changes. Rails 2.2 includes transactional migrations for PostgreSQL. Rails 2.3 includes transactional migrations for SQLite. This doesn’t really help you for your problem right now, but if you have a choice of database on future projects, I recommend … Read more
in rails 5.x you can add a foreign key to a table with a different name like this: class AddFooBarStoreToPeople < ActiveRecord::Migration[5.0] def change add_reference :people, :foo_bar_store, foreign_key: { to_table: :stores } end end Inside a create_table block t.references :feature, foreign_key: {to_table: :product_features}
For many operations rails can guess what is the inverse operation (without problems). For example, in your case what is the reverse operation of add_column to call when you rollback? Of course it’s remove_column. What is the inverse of create_table? It’s drop_table. So in these cases rails know how to rollback and define a down … Read more
Looking at the source code, they do the same exact thing — belongs_to is an alias of reference: def references(*args) options = args.extract_options! polymorphic = options.delete(:polymorphic) args.each do |col| column(“#{col}_id”, :integer, options) column(“#{col}_type”, :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil? end end alias :belongs_to :references This is just a way of making your code … Read more
What you are trying to do is not yet implemented in the stable version of rails so Michelle’s answer is the right one for now. But this feature will be implemented in rails 4 and is already available in the edge version as follows (according to this CHANGELOG): $ rails generate migration AddImageableToProducts imageable:references{polymorphic} Some … Read more