How do I save a row update using Sequel?

I really like the simplicity of Sequel [1], but right now it is
giving me fits. After I insert a row into the database, I can’t seem
to get it to persist any updates that I make to that row. Here’s an
example.

[cremes@calvin tmp]$ sqlite3 test.db
SQLite version 3.5.1
Enter “.help” for instructions
sqlite> create table test_table (a text, b text, c text);
sqlite> .tables
test_table
[cremes@calvin tmp]$ sequel ‘sqlite:///test.db’
Your database is stored in DB…
irb(main):001:0> d = DB[:test_table]
=> #<Sequel::SQLite::Dataset:0x2aaaab5486e0 @columns=nil, @opts=
{:from=>[:test_table]}, @row_proc=nil, @db=#<Sequel::SQLite::Database:
0x2af90882ab50 @pool=#<Sequel::ConnectionPool:0x2af90882aad8
@available_connections=[#<SQLite3::Database:0x2af90882a880
@type_translation=true, @driver=#<SQLite3::Driver::Native::Driver:
0x2af9088136d0 @authorizer={}, @callback_data={}, @trace={},
@busy_handler={}>, @statement_factory=SQLite3::Statement,
@results_as_hash=false,
@handle=#SWIG::TYPE_p_sqlite3:0x2af908813590,
@transaction_active=false, @closed=false, @translator=nil>],
@connection_proc=#<Proc:0x00002af9073d8198@/usr/lib/ruby/gems/1.8/
gems/sequel-0.3.0.1/lib/sequel/database.rb:27>, @created_count=1,
@mutex=#Mutex:0x2af90882aa88, @allocated={}, @max_size=4>, @opts=
{:password=>nil, :host=>nil, :port=>nil, :database=>“test.db”, :user=>ni
l}, @logger=nil, @single_threaded=false>>
irb(main):002:0> d.all.count
NoMethodError: undefined method `count’ for []:Array
from (irb):2
irb(main):003:0> d.all
=> []
irb(main):004:0> d << {:a => ‘sample1’, :b => ‘sample2’}
=> 1
irb(main):005:0> d.all
=> [{:b=>“sample2”, :a=>“sample1”, :c=>nil}]
irb(main):006:0> d.filter(‘a = ?’, ‘sample1’).order(:b).last.update
(:c => ‘sample3’)
=> {:b=>“sample2”, :a=>“sample1”, :c=>“sample3”}
irb(main):007:0> d.all
=> [{:b=>“sample2”, :a=>“sample1”, :c=>nil}]
irb(main):008:0>

Notice how ‘sample3’ goes missing after the +update+ method executes.
The result from that call clearly shows a hash with that field
updated, but when I ask the database to print that row it doesn’t
show up. I tried executing a +save+ but that method isn’t defined on
Array.

Can anyone help me out?

cr

On Oct 22, 11:14 pm, Chuck R. [email protected] wrote:

I really like the simplicity of Sequel [1], but right now it is
giving me fits. After I insert a row into the database, I can’t seem
to get it to persist any updates that I make to that row. Here’s an
example.

Hi Chuck.

You can post questions about Sequel to the sequel-talk group:

http://groups.google.com/group/sequel-talk?hl=en

Regarding your problem, when you get back records as a bunch of hashes
from a dataset, you can’t update them, they’re just stupid Ruby hashes
that don’t know anything about databases.

What you should use is the Sequel model class, which knows about
primary keys and all that stuff. Here’s an example:

class TestItem < Sequel::Model(:test_table)
# in order to be able to update records you must use
# some kind of primary key. By default the primary
# key is :id, but you can set it to anything you want
# including composite primary keys.
set_primary_key :a
end

TestItem.create(:a => ‘sample1’, :b => ‘sample2’)
t = TestItem.order(:b).last
t.set(:c => ‘sample3’)

best
Sharon