Search code examples
rubysqlitesequel

can't 'delete' in Ruby Sequel


I'm starting with the Sequel database library for Ruby.

Given the following code:

require 'sequel'
db = Sequel.connect('sqlite://users.db')
users = db[:users]
users.first.delete

Ruby throws the error:

'delete' : wrong number of arguments (0 for 1) (ArgumentError)

I can delete multiple records, but .first.delete doesn't work. What's wrong?


Solution

  • db[:users] gives you an Sequel::SQLite::Dataset. users.first method without any argument gives you a user as Hash object. Now you are calling Hash#delete, which required a argument. But you are not passing it that argument. Thus it is complaining.

    Rather do as below :

    # if you want to delete the the first matching argument of any specific
    # attribute/db column
    db[:users].filter(:key => 1).delete
    # or if you want to delete the first record from db
    db[:users].limit(1).delete
    

    Read this #drop method also.