Forum: Ruby on Rails Easy way...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ead43bcb0202410540a6a7cee5955d46?d=identicon&s=25 Fresh Mix (giga)
on 2008-11-25 17:10
Best way to do this:

Pseudo code

(word is unique)

1 Select * from words where word = "test"
2 If result > 0 then update words set count = coun +1
3 else Insert into words (word, count) values ("test", 1)
Ead43bcb0202410540a6a7cee5955d46?d=identicon&s=25 Fresh Mix (giga)
on 2008-11-25 17:11
Sorry

> 2 If result > 0 then update words set count = coun + 1 where word = "test"
F4ae71a145a8960a4c29692c96e69dee?d=identicon&s=25 Gabriel Laskar (Guest)
on 2008-11-25 18:19
(Received via mailing list)
On Tue, Nov 25, 2008 at 5:10 PM, Gi Ga
<rails-mailing-list@andreas-s.net> wrote:
>
> Best way to do this:
>
> Pseudo code
>
> (word is unique)
>
> 1 Select * from words where word = "test"
> 2 If result > 0 then update words set count = coun +1
> 3 else Insert into words (word, count) values ("test", 1)

class Word < ActiveRecord::Base
end

word = Word.find_or_create_by_name("test")
word.count = word.count + 1
word.save

will propably do the job

--
Gabriel Laskar <bibi.skuk@gmail.com>
This topic is locked and can not be replied to.