I’m trying to update field on duplicate key, but it just adds new records instead of updating.
Here is what i’m trying to do
ActiveRecord::Base.connection.execute "insert into open_offer_counters (offer_id, udid, open_counter) values #{opened_events.join(", ")} on duplicate key update open_counter = open_counter + 1"
In my migration I pointed on 2 unique fields
create_table :open_offer_counters do |t|
t.integer :offer_id, :unique => true
t.string :udid, :unique => true
t.integer :open_counter
t.timestamps
end
Any ideas whats wrong with code?
You need to add a unique index on these columns, such as
Passing :unique as a column option doesn’t do anything