avoid exception for general case

This commit is contained in:
Sam 2016-04-06 13:18:11 +10:00
parent bc97e09dde
commit 4f10b5e940
1 changed files with 3 additions and 0 deletions

View File

@ -5,6 +5,9 @@ class UserFirst < ActiveRecord::Base
end
def self.create_for(user_id, type, post_id=nil)
# the usual case will be that it is already in table, don't try to insert
return false if UserFirst.exists?(user_id: user_id, first_type: types[type])
create!(user_id: user_id, first_type: types[type], post_id: post_id)
true
rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique