Skip to content

ActiveRecord#fetch_or_initialize and ActiveRecord#fetch_or_create for IdentityCache

License

Notifications You must be signed in to change notification settings

KevinBongart/identity_cache_fetchers

Repository files navigation

IdentityCacheFetchers Circle CI Code Climate

ActiveRecord#fetch_or_initialize and ActiveRecord#fetch_or_create for IdentityCache

# based on https://github.com/Shopify/identity_cache#secondary-indexes
class Product < ActiveRecord::Base
  include IdentityCache
  include IdentityCacheFetchers

  cache_index :handle, unique: true
  cache_index :vendor, :product_type
end

This gem brings you the equivalent of Ruby on Rails' find_or_initialize and find_or_create but uses IdentityCache as a first source:

Product.fetch_or_initialize_by(handle: "test")
Product.fetch_or_initialize_by(vendor: "Acme Corporation", product_type: "A thing")
Product.fetch_or_create_by(handle: "test")
Product.fetch_or_create_by(vendor: "Acme Corporation", product_type: "A thing")

Time to go crazy:

gem 'identity_cache_fetchers'

About

ActiveRecord#fetch_or_initialize and ActiveRecord#fetch_or_create for IdentityCache

Resources

License

Stars

Watchers

Forks

Packages

No packages published