I’d like to use an if statement to check if the value in a Postgres DB table is unique. If unique, then do something, if not unique, do something else. Here’s what the pseudo code would look like in Ruby on Rails.
if validates_uniqueness_of :number == "true"
puts "this value is unique and should be added to the DB"
else
puts "this value is not unique and should not be added to the DB"
end
Can this type of logic be implemented in the model or controller? If yes, which is the better way to go? If no, what should I do instead? Also, what would the syntax look for something like this?
Thanks guys!
You can use the exists? method to check whether a record is in the database already. It can take a hash of fields you want to search on: