On Mon, Jun 22, 2009 at 12:06 PM, Valentino
Lun<[email protected]> 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
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---