In my app, I want to create a new record if it doesn't exist, and update
some fields if it does.

Looking at the debug log, and it appears that I'm doing something that's
causing an extra SELECT.  The basic synopsis:

  sta = Station.find_by_station_id(station_id)
  if sta.nil?
    Station.create(params)
  else
    sta.p1 = "white port"
    sta.p2 = "lemon juice"
    sta.save!
  end

The Station.create() isn't a problem.  But if it takes the 'else'
clause, the SQL trace looks suspicious.  (Hand edited for clarity...):
=============
Station Load: SELECT * FROM `stations` WHERE (`stations`.`station_id` =
'WPLJ') LIMIT 1

SQL BEGIN

Station Load: SELECT `stations`.id FROM `stations` WHERE
(`stations`.`station_id` = BINARY 'WPLJ' AND `stations`.id <> 12095)
LIMIT 1

Station Update: UPDATE `stations` SET `p1` = "white port", `p2` = "lemon
juice" WHERE `id` = 12095

COMMIT
=============
It seems like just the UPDATE would suffice, so what is that second
SELECT doing?  And what can I do to make it go away? :)

TIA.

- ff
-- 
Posted via http://www.ruby-forum.com/.

-- 
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.

Reply via email to