Last active
April 19, 2017 03:30
-
-
Save YoshitsuguFujii/aaedadf857e9c724881462671ba5c083 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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