Wednesday, January 4, 2012

Re: [Rails] AR update / create pattern: is there an easier way?

On 4 January 2012 20:25, Fearless Fool <lists@ruby-forum.com> wrote:
> I find myself writing the following frequently:
>
>    r = MyActiveRecord.where(:attr1 => cond1, :attr2 => cond2, ...)
>    r.exists? ? r.first : r.create

You want to look at the create_or_update_by_id and
find_or_initialize_by_id methods :-)

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment