]> git.openstreetmap.org Git - rails.git/blob - db/migrate/20161002153425_add_join_table_between_users_and_diary_entries.rb
cd8414ee7ece4518ccb5b24497910cc409802e50
[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_entries_subscribers, :id => false do |t|
4       t.column :subscriber_id, :bigint, :null => false
5       t.column :diary_entry_id, :bigint, :null => false
6     end
7
8     add_foreign_key :diary_entries_subscribers, :users, :column => :subscriber_id, :name => "diary_entries_subscribers_subscriber_id_fkey"
9     add_foreign_key :diary_entries_subscribers, :diary_entries, :column => :diary_entry_id, :name => "diary_entries_subscribers_changeset_id_fkey"
10
11     add_index :diary_entries_subscribers, [:subscriber_id, :diary_entry_id], :unique => true, :name => "index_diary_subscribers_on_subscriber_id_and_diary_id"
12     add_index :diary_entries_subscribers, [:diary_entry_id]
13   end
14
15   def up
16     DiaryEntry.find_each do |diary_entry|
17       diary_entry.subscribers << diary_entry.user unless diary_entry.subscribers.exists?(diary_entry.user.id)
18     end
19   end
20
21   def down
22   end
23 end