1234567891011121314151617181920212223242526272829 |
- class AddEventIdAtCreationToLinks < ActiveRecord::Migration
- class Link < ActiveRecord::Base; end
- class Event < ActiveRecord::Base; end
- def up
- add_column :links, :event_id_at_creation, :integer, :null => false, :default => 0
- Link.all.find_each do |link|
- last_event_id = execute(
- <<-SQL
- SELECT
- FROM
- WHERE events.agent_id =
- SQL
- ).first.to_a.first
- if last_event_id.nil?
- link.event_id_at_creation = Event.last.id
- else
- link.event_id_at_creation = last_event_id
- end
- link.save
- end
- end
- def down
- remove_column :links, :event_id_at_creation
- end
- end
|