Adds uniqueness constraint on paper-SNP connection. Replaces rake task by SQL command.

This commit is contained in:
Philipp Bayer
2015-10-28 21:03:34 +08:00
parent f9f41dc11b
commit 2d03669002
2 changed files with 6 additions and 5 deletions

View File

@@ -3,4 +3,5 @@ class SnpReference < ActiveRecord::Base
belongs_to :snp
belongs_to :paper, polymorphic: true
#validates_presence_of :snp, :paper
validates_uniqueness_of :snp_id, scope: [:paper_id, :paper_type]
end

View File

@@ -1,9 +1,9 @@
namespace :papers do
task :make_linked_snps_unique => :environment do
%w(MendeleyPaper SnpediaPaper PlosPaper).each do |source|
source.constantize.find_each do |s|
s.update(snps: s.snps.uniq)
end
end
ActiveRecord::Base.connection.execute(<<-SQL)
SELECT DISTINCT * INTO new_table FROM snp_references;
DROP TABLE snp_references;
ALTER TABLE new_table RENAME TO snp_references;
SQL
end
end