Displaying 1 result from an estimated 1 matches for "create_or_update_by_id".
2012 Jan 04
1
AR update / create pattern: is there an easier way?
I find myself writing the following frequently:
r = MyActiveRecord.where(:attr1 => cond1, :attr2 => cond2, ...)
r.exists? ? r.first : r.create
... which has the effect of returning an instance of MyActiveRecord if
all the conditions are met, or creating one if it doesn''t. A variant of
this:
c = MyActiveRecord.new(:attr1 => val1, :attr2 => val2, ...)
r =