Skip to content

Instantly share code, notes, and snippets.

@apangeajwrubel
Created August 28, 2012 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apangeajwrubel/3503792 to your computer and use it in GitHub Desktop.
Save apangeajwrubel/3503792 to your computer and use it in GitHub Desktop.
updating version_of_ attributes
std = $stdout
$stdout = File.new('console.out', 'w')
ActiveRecord::Base.logger = nil
Item.published.all.each do |item|
next if (item.active == false)
#item = Item.find_by_item_number_and_state(60124, "Published")
# pp item.item_number
item.working_version.item_steps.each do |item_step_working|
item_step_published = item_step_working.item.published_version.item_steps.find_by_sort_order(item_step_working.sort_order)
# this is a silly way to see if item_steps, hints and response positions match up
if (item_step_published.blank?)
pp "WARNING: Item Number #{item.item_number} - item_step sort_orders do not match up"
next
end
if (item_step_working.hints.sum('position') != item_step_published.hints.sum('position'))
pp "WARNING: Item Number #{item.item_number} - hint positions do not match up"
next
end
if (item_step_working.responses.sum('position') != item_step_published.responses.sum('position'))
pp "WARNING: Item Number #{item.item_number} - response positions do not match up"
next
end
begin
item_step_working.update_attributes!(version_of_item_step_id: item_step_published.id)
rescue => msg
pp "ERROR: #{msg.to_s}"
end
item_step_working.hints.each do |h|
h.version_of_hint_id = item_step_published.hints.find_by_position(h.position).id
begin
h.save
rescue => msg
pp "ERROR: #{msg.to_s}"
end
end
item_step_working.responses.each do |r|
r.version_of_response_id = item_step_published.responses.find_by_position(r.position).id
begin
r.save
rescue => msg
pp "ERROR: #{msg.to_s}"
end
end
end
end
$stdout.sync = true
$stdout = std
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment