Module: Sequel::Plugins::UpdateOrCreate::ClassMethods
- Defined in:
- lib/sequel/plugins/update_or_create.rb
Instance Method Summary collapse
-
#find_or_new(attrs, set_attrs = nil) {|obj| ... } ⇒ Object
Operates the same as
update_or_create
, but returns the objects without persisting changes (no UPDATE/INSERT queries). -
#update_or_create(attrs, set_attrs = nil, &block) ⇒ Object
Attempt to find an record with the
attrs
, which should be a hash with column symbol keys.
Instance Method Details
#find_or_new(attrs, set_attrs = nil) {|obj| ... } ⇒ Object
Operates the same as update_or_create
, but returns the objects without persisting changes (no UPDATE/INSERT queries).
55 56 57 58 59 60 |
# File 'lib/sequel/plugins/update_or_create.rb', line 55 def find_or_new(attrs, set_attrs=nil) obj = find(attrs) || new(attrs) obj.set(set_attrs) if set_attrs yield obj if defined?(yield) obj end |
#update_or_create(attrs, set_attrs = nil, &block) ⇒ Object
Attempt to find an record with the attrs
, which should be a hash with column symbol keys. If such an record exists, update it with the values given in set_attrs
. If no such record exists, create a new record with the columns specified by both attrs
and set_attrs
, with the ones in set_attrs
taking priority. If a block is given, the object is yielded to the block before the object is saved. Returns the new or updated object.
47 48 49 50 51 |
# File 'lib/sequel/plugins/update_or_create.rb', line 47 def update_or_create(attrs, set_attrs=nil, &block) obj = find_or_new(attrs, set_attrs, &block) obj.save_changes obj end |