docbrown/db/migrate/20200609192545_create_user_subscriptions.rb
Alex 6af063eebf
[deploy] Email signups liquid tag backend (#8375)
* Backend MVP

- Migration
- Model
- Associations

* Add factory, spec, and validations

- Add unqiue index for validations

* Fix specs

* Add author to email_subscriptions

* Add user association to set_author_id

* Add clarifying comment to set_author_id

* Writing is hard sometimes

* Rename email_subscriptions to subscription_sources

* UserSubscription and user_subscription_sourceable

* subscribers --> sourced_subscribers

* Add comment to explain UserSubscription model
2020-06-15 09:33:34 -04:00

18 lines
738 B
Ruby

class CreateUserSubscriptions < ActiveRecord::Migration[6.0]
def change
create_table :user_subscriptions do |t|
t.references :user_subscription_sourceable, polymorphic: true, null: false, index: { name: :index_on_user_subscription_sourcebable_type_and_id }
t.references :subscriber, references: :users, foreign_key: { to_table: :users }, null: false
t.references :author, references: :users, foreign_key: { to_table: :users }, null: false
t.timestamps
end
add_index(
:user_subscriptions,
%i[subscriber_id user_subscription_sourceable_id user_subscription_sourceable_type],
unique: true,
name: :index_on_subscriber_id_user_subscription_sourceable_type_and_id
)
end
end