]> git.openstreetmap.org Git - rails.git/blob - db/migrate/20161002153425_add_join_table_between_users_and_diary_entries.rb
more idiomatic models for diary entry subscriptions
[rails.git] / db / migrate / 20161002153425_add_join_table_between_users_and_diary_entries.rb
1 class AddJoinTableBetweenUsersAndDiaryEntries < ActiveRecord::Migration
2   def change
3     create_table :diary_entry_subscriptions, :id => false do |t|
4       t.column :user_id, :bigint, :null => false
5       t.column :diary_entry_id, :bigint, :null => false
6     end
7
8     add_index :diary_entry_subscriptions, [:user_id, :diary_entry_id], :unique => true, :name => "index_diary_subscriptions_on_user_id_and_diary_entry_id"
9     add_index :diary_entry_subscriptions, [:diary_entry_id]
10   end
11
12   def up
13     DiaryEntry.find_each do |diary_entry|
14       diary_entry.subscriptions.create(user: diary_entry.user) unless diary_entry.subscribers.exists?(@user)
15     end
16   end
17
18   def down
19   end
20 end