# Thinking About Rails Database Objects and Idempotency

DevFeed: [Thinking About Rails Database Objects and Idempotency](<https://devfeed.tech/articles/thinking-about-rails-database-objects-and-idempotency-28283.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rails/2022/06/10/thinking-about-rails-database-objects-and-idempotency.html>)

Author: Fuzzygroup

Published: 2022-06-10T16:47:00Z

Content type: opinion

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Database](<https://devfeed.tech/topics/database.md>), [Development](<https://devfeed.tech/topics/development.md>), [Code](<https://devfeed.tech/topics/code.md>), [debugging](<https://devfeed.tech/topics/debugging.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [database](<https://devfeed.tech/tags/database.md>), [debugging](<https://devfeed.tech/tags/debugging.md>), [development](<https://devfeed.tech/tags/development.md>), [rails](<https://devfeed.tech/tags/rails.md>)

## AI overview

A personal technical discussion of idempotency in Rails database objects. The author considers replacing class-level find_or_create methods with class-defined identity columns that determine whether a record already exists, using CodeEnvironmentLogin as an example.

## Source excerpt

I've been dealing with a new application with a lot of objects and one of my concerns is idempotency. Idempotency is the idea that you can do the same thing over and over and only create a new object when Idempotence (UK: /ˌɪdɛmˈpoʊtəns/,[1] US: /ˈaɪdəm-/)[2] is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application. Idempotence Specifically if I'm saving a login and password to a database for a certain project and environment, I only want to save that one time. I normally handle this with a class level find_or_create method but that, in 2022, is feeling unexpectedly clunky. It is unclear to me what the current thinking in the Rails world is regarding low level operations like this - my last technical conference was now in 2016 (and that was for Elixir not even Ruby). As with so many blog posts, I'm going to sketch out my solution here in the hopes that writing it all down: Kick starts the brain Puts something out there to spur a conversation It seems to me that idempotency varies on a class level. For an account class just the email field might make it idempotent. For my CodeEnvironmentLogin class, it is going to be several fields: code_environment_id login project_id Note: And, yes, I'm building a development tool as this blog post absolutely reveals. The fact that this varies on a class level basis to me argues for a class level constant. Let's call them "identity columns" since they uniquely identify records: IDENTITY_COLUMNS = [:project_id, :code_environment_id, :code_environment_login] Now what we need is a method which can: read the identity columns generate a where clause using a passed in OpenStruct (all my find_or_create methods us an OpenStruct) return true or false if it exists and the object itself Here's my first stab at it: def self.exists?(struct) where_clauses = [] IDENTITY_COLUMNS.each do |identity_column| where_clauses << {identity