On Mon, Jun 22, 2009 at 12:06 PM, Valentino
Lun<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
> Dear all
>
> Are there any method to create or update something? (if something exist,
> then update, else create....where something is the primary key)
>
> Something like....
> if Klass.find_by_something( xxx )
> Klass.update_attribute your_hash
> else
> Klass.create your_hash
> end
>
> This is similar to the AR dynamic finder
> Klass.find_or_create_by_something()..., but I want a
> Klass.create_or_update_by_something...Any idea??
>
> Many thanks
> Valentino
I don''t think so, how about:
Klass.send("find_or_create_by_#{attributes.map{|k,v|
k}.join("_and_")}", *attributes.map{|k,v| v})
WARNING: This is a quick solution and could break because I''m relying
on the fact that the hash will return in the same order for both calls
Andrew Timberlake
http://ramblingsonrails.com
http://MyMvelope.com - The SIMPLE way to manage your savings