I can't seem to shake this exception. I have the following code:
class Batch < ApplicationRecord before_create :upcase_session_id def self.for_session(session_id, opts) batch = Batch.where(session_id: session_id.upcase).first_or_initialize if batch.new_record? batch.property = opts[:property] batch.save! end batch end private def upcase_session_id self.session_id = session_id.upcase end end
Calling Code:
def retrieve_batch self.batch ||= Batch.for_session(batch_session_id, property: property) end
I'm not sure how I can change this code to stop getting this exception that I keep getting.
1 Answers
Answers 1
The exception is raised when a record cannot be inserted because it would violate a uniqueness constraint. You should tell us the table constraints because the problem could be any column.
You could try this:
class Batch < ApplicationRecord def self.for_session(session_id, opts) Batch.where(session_id: session_id.upcase).first_or_create(property: opts[:property]) end end
0 comments:
Post a Comment