On May 28, 10:38 pm, Joel VanderWerf <[email protected]> wrote:
> (1) why does
>
>      entries = items.where("id" => id)
>
> fail, when "id" is apparently an acceptable substitute for :id in #<< ?

Because "id" in the filter is an SQL string.  If you want an SQL
identifier, you should use :id.  The only reason "id" works in insert/
<< is that you can't use an SQL string in its place, so it assumes you
mean an identifer:

  UPDATE items SET 'id' = 3; -- Invalid SQL

  items.where(:id => id)  # WHERE id = 3
  items.where('id' => id) # WHERE 'id' = 3

In general you should always use symbols for SQL identifiers (columns/
tables) when you are using Sequel.

> (2) Is there a way to express the if... as a single operation, with the
> same semantics (i.e. not failing when there's already an entry)?

If you are using MySQL, you can use:

  ds[:items].replace(:id=>id)

That'll use a single REPLACE query.  REPLACE isn't standard SQL
though, and I don't think any other adapters support it.  Models have
a find_or_create method which does something similar.  Your way isn't
a bad way to do things.

Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-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/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to