Skip to content

Instantly share code, notes, and snippets.

@crova
Created June 10, 2016 18:57
Show Gist options
  • Save crova/7a78bd5428b876103e6b75f6470199e0 to your computer and use it in GitHub Desktop.
Save crova/7a78bd5428b876103e6b75f6470199e0 to your computer and use it in GitHub Desktop.
How to update rather than create if ID matches?
# fetchs SIB Campaigns
def fetch
sib = Mailin.new("xxx","xxx")
data = { "type"=>"classic", "status" => "sent", "page"=>1,"page_limit"=>1000 }
shoots = sib.get_campaigns_v2(data)
shoots = shoots.dig('data')
rst = shoots.dig('campaign_records')
# update/create
rst.each do |row|
shoot = Shoot.find_by_id(:id) || Shoot.new
shoot.attributes = row
shoot.save!
end
redirect_to(:action => 'index')
end
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "shoots_pkey" DETAIL: Key (id)=(279) already exists.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment