Skip to content

Instantly share code, notes, and snippets.

@D1plo1d
Created April 3, 2014 21:58
Show Gist options
  • Save D1plo1d/9963735 to your computer and use it in GitHub Desktop.
Save D1plo1d/9963735 to your computer and use it in GitHub Desktop.
push_and_dont_save.rb
module ActiveRecord
module Associations
class HasManyAssociation < CollectionAssociation
# Pushes a model into the association but does not save it to the database
def push_and_dont_save(record)
@target.push record
set_owner_attributes(record)
set_inverse_instance(record)
end
end
class CollectionProxy < Relation
# Creating an alias in collection proxy to it's proxied push_and_dont_save
def push_and_dont_save(record)
return unless proxy_association.respond_to? :push_and_dont_save
proxy_association.push_and_dont_save record
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment