Skip to content

Instantly share code, notes, and snippets.

@craic
Created February 10, 2014 22:29
Show Gist options
  • Save craic/8925564 to your computer and use it in GitHub Desktop.
Save craic/8925564 to your computer and use it in GitHub Desktop.
# Database
add column 'sequence_type_tmp'
# Model
# original is :sequence_type_id (integer)
# add :sequence_type_tmp (string)
@sequence_types = %w{ protein dna rna }
@sequence_type_select = [['Protein', 'protein'], ['DNA', 'dna'], ['RNA', 'rna']]
class << self
attr_accessor :sequence_types, :sequence_type_select
end
# Rake task
namespace :craic do
desc "Convert sequence_type_id into sequnce_type"
task :convert_sequence_type_ids => :environment do
Antibody.order('id asc').all.each do |antibody|
unless antibody.sequence_type.blank?
antibody.update_attribute(:sequence_type_tmp, antibody.sequence_type.name)
end
end
end
end
# THEN....
# remove sequence_type_id column
# change sequence_type_tmp column to sequence_type
# update model to use sequence_type instead of sequence_type_tmp
# remove belongs_to sequence_type relation and has_many :antibodies in sequence_type model
# View -- form
<%= f.input :sequence_type, :collection => Sequence.sequence_type_select,
:selected => 'protein' %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment