Skip to content

Instantly share code, notes, and snippets.

@YoshitsuguFujii
Last active April 19, 2017 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoshitsuguFujii/aaedadf857e9c724881462671ba5c083 to your computer and use it in GitHub Desktop.
Save YoshitsuguFujii/aaedadf857e9c724881462671ba5c083 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module Refinements
module AssociationCollectionExtension
refine ActiveRecord::Associations::CollectionProxy do
def find_or_build_by(arguments)
find_by(arguments) || build(arguments)
end
def detect_or_build_by(arguments)
self.detect do|instance|
arguments.all? do |key, value|
instance.send(key) == value
end
end || build(arguments)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment