migrations
add_column add_foreign_key add_index add_reference add_timestamps change_column_default (must supply a :from and :to option) change_column_null create_join_table create_table disable_extension drop_join_table drop_table (must supply a block) enable_extension remove_column (must supply a type) remove_foreign_key (must supply a second table) remove_index remove_reference remove_timestamps rename_column rename_index rename_table
Action Text Scaffold generator
bin/rails generate model <ModelName> <association_name>:rich_text
References
rails generate model api_connection user:references key:string class CreateParentAccountTypes < ActiveRecord::Migration[5.2] def change create_table :parent_account_types do |t| t.references :account, foreign_key: true t.references :student_account, foreign_key: {to_table: :accounts } t.string :permission, default: 'pending' t.timestamps end end end ``` ```ruby class ParentAccountType < ApplicationRecord belongs_to :account belongs_to :student_account, class_name: Account.name end ```
Add reference
```ruby class AddCreatorToLinks < ActiveRecord::Migration[5.2] def change add_reference :links, :creator, index: true, foreign_key: {to_table: :accounts } end end ``` ```ruby class Link belongs_to :creator, class_name: 'Account' end ``` If you need to change from one referenece to another ```ruby class AddCreatorToLinks < ActiveRecord::Migration[5.2] def change remove_reference :links, :creator, index: true, foreign_key: {to_table: :accounts } add_reference :links, :account, index: true, foreign_key: true end end ```
References polymorphic
$ rails g model ModelPointingToPolymProperty property:references{polymorphic}
or
class CreateImages < ActiveRecord::Migration[6.0] def change create_table :images do |t| t.references :imageable, polymorphic: true end end endclass AddImageableToImages < ActiveRecord::Migration def change add_reference :images, :imageable, polymorphic: true, index: true end end
----------------------------
``` Table "public.parent_account_types" Column | Type | Collation | Nullable | Default -------------------------+-----------------------------+-----------+----------+-------------------------------------------------- id | bigint | | not null | nextval('parent_account_types_id_seq'::regclass) account_id | bigint | | | permission | character varying | | | 'pending'::character varying created_at | timestamp without time zone | | not null | updated_at | timestamp without time zone | | not null | student_account_type_id | bigint | | | Indexes: "parent_account_types_pkey" PRIMARY KEY, btree (id) "index_parent_account_types_on_account_id" btree (account_id) "index_parent_account_types_on_student_account_type_id" btree (student_account_type_id) Foreign-key constraints: "fk_rails_774a46bba8" FOREIGN KEY (student_account_type_id) REFERENCES student_account_types(id) "fk_rails_d30cf53e1f" FOREIGN KEY (account_id) REFERENCES accounts(id) ``` ``` Table "public.parent_account_types" Column | Type | Collation | Nullable | Default -------------------------+-----------------------------+-----------+----------+-------------------------------------------------- id | bigint | | not null | nextval('parent_account_types_id_seq'::regclass) account_id | bigint | | | permission | character varying | | | 'pending'::character varying created_at | timestamp without time zone | | not null | updated_at | timestamp without time zone | | not null | student_account_type_id | bigint | | | Indexes: "parent_account_types_pkey" PRIMARY KEY, btree (id) "index_parent_account_types_on_account_id" btree (account_id) "index_parent_account_types_on_student_account_type_id" btree (student_account_type_id) Foreign-key constraints: "fk_rails_774a46bba8" FOREIGN KEY (student_account_type_id) REFERENCES student_account_types(id) "fk_rails_d30cf53e1f" FOREIGN KEY (account_id) REFERENCES accounts(id) ``` # rename column ``` pobble-v3_test=# \d parent_account_types Table "public.parent_account_types" Column | Type | Collation | Nullable | Default -------------------------+-----------------------------+-----------+----------+-------------------------------------------------- id | bigint | | not null | nextval('parent_account_types_id_seq'::regclass) account_id | bigint | | | permission | character varying | | | 'pending'::character varying created_at | timestamp without time zone | | not null | updated_at | timestamp without time zone | | not null | student_account_type_id | bigint | | | Indexes: "parent_account_types_pkey" PRIMARY KEY, btree (id) "index_parent_account_types_on_account_id" btree (account_id) "index_parent_account_types_on_student_account_type_id" btree (student_account_type_id) Foreign-key constraints: "fk_rails_774a46bba8" FOREIGN KEY (student_account_type_id) REFERENCES student_account_types(id) "fk_rails_d30cf53e1f" FOREIGN KEY (account_id) REFERENCES accounts(id) pobble-v3_test=# \d parent_account_types Table "public.parent_account_types" Column | Type | Collation | Nullable | Default -------------------------+-----------------------------+-----------+----------+-------------------------------------------------- id | bigint | | not null | nextval('parent_account_types_id_seq'::regclass) parent_id | bigint | | | permission | character varying | | | 'pending'::character varying created_at | timestamp without time zone | | not null | updated_at | timestamp without time zone | | not null | student_account_type_id | bigint | | | Indexes: "parent_account_types_pkey" PRIMARY KEY, btree (id) "index_parent_account_types_on_parent_id" btree (parent_id) "index_parent_account_types_on_student_account_type_id" btree (student_account_type_id) Foreign-key constraints: "fk_rails_774a46bba8" FOREIGN KEY (student_account_type_id) REFERENCES student_account_types(id) "fk_rails_d30cf53e1f" FOREIGN KEY (parent_id) REFERENCES accounts(id) ``` ; ``` class RenameAccountToParentInParentType < ActiveRecord::Migration[5.2] def change rename_column :parent_account_types, :account_id, :parent_id end end ``` ``` create_table :login_codes do |t| t.references :resource, index: true, polymorphic: true t.string :code, index: true t.datetime :expire_at t.datetime :used_at t.timestamps end def change add_reference :property_primitives, :owner, index: true, polymorphic: true end ``` will generate ``` create_table "login_codes", force: :cascade do |t| t.string "resource_type" t.bigint "resource_id" t.string "code" t.datetime "expire_at" t.datetime "used_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["code"], name: "index_login_codes_on_code" t.index ["resource_type", "resource_id"], name: "index_login_codes_on_resource_type_and_resource_id" end ``` has_and_belongs_to_many :groups, -> { distinct } has_and_belongs_to_many :student_account_types, -> { distinct } class CreateDomains < ActiveRecord::Migration[6.0] def change create_table :domains do |t| t.references :company, index: { unique: true } t.string :name t.string :public_uid, index: { unique => true } t.timestamps end end end rails g model article_tag article:reference tag:reference