has_many, belongs_to relation in active record migration rails 4

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

Where is the documentation page for ActiveRecord data types?

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

Rolling back a failed Rails migration

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

Rails Migration: add_reference to Table but Different Column Name For Foreign Key Than Rails Convention

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}

Rails migrations: self.up and self.down versus change

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

What is the difference between t.belongs_to and t.references in rails?

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

how to generate migration to make references polymorphic

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

Specifying column name in a “references” migration

For Rails 5+ Initial Definition: If you are defining your Post model table, you can set references, index and foreign_key in one line: t.references :author, index: true, foreign_key: { to_table: :users } Update Existing: If you are adding references to an existing table, you can do this: add_reference :posts, :author, foreign_key: { to_table: :users } … Read more