Created
June 10, 2016 18:57
-
-
Save crova/7a78bd5428b876103e6b75f6470199e0 to your computer and use it in GitHub Desktop.
How to update rather than create if ID matches?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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