odd number of arguments for Hash

10 views
Skip to first unread message

bee...@gmail.com

unread,
Aug 28, 2021, 5:26:42 PMAug 28
to sequel-talk
Not sure why this is happening.  I'm getting <subject> as an error.  I've tried using both directive and non-directive for @id in the initialize method, with same error.  Seems the object is the same column profile.  Any clue why this is happening?


DL.create_table? :mobibase do
  primary_key   :id

  DateTime      :cdate
  String        :label, size: 200
  String        :note, size: 350

  String        :status, size: 20, default: 'new'
end

class Mobibase < Sequel::Model(DL[:mobibase])

  def initialize(cdate, label, note)
#     @id = nil
    @cdate = cdate
    @label = label
    @note = note
    @status = 'new'
    puts "A new instantiation"
  end
      
  def inspect
    self.instance_variables.each do |i|
      puts "#{ i }: #{ instance_variable_get(i) }"
    end
  end
  
end

puts "db columns:"
puts DL[:mobibase].columns
item = Mobibase.new(cdate = 'Aug 27, 2021', label = 'My label', note = 'Bravo, well done')
item.inspect
puts
item.save


Results in...

db columns:
id
cdate
label
note
status

A new instantiation
@cdate: Aug 27, 2021
@label: My label
@note: Bravo, well done
@status: new

Traceback (most recent call last):
18: from /users/rich/app.rb:301:in `<main>'
17: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1488:in `save'
16: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1930:in `checked_save_failure'
15: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1488:in `block in save'
14: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1942:in `checked_transaction'
13: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/database/transactions.rb:195:in `transaction'
12: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/database/connecting.rb:269:in `synchronize'
11: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/connection_pool/threaded.rb:92:in `hold'
10: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/database/transactions.rb:233:in `block in transaction'
9: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/database/transactions.rb:258:in `_transaction'
8: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1942:in `block in checked_transaction'
7: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1488:in `block (2 levels) in save'
6: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1776:in `_save'
5: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1038:in `around_save'
4: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1794:in `block in _save'
3: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1038:in `around_update'
2: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1802:in `block (2 levels) in _save'
1: from /Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1846:in `_save_update_all_columns_hash'
/Users/rich/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/sequel-5.46.0/lib/sequel/model/base.rb:1846:in `[]': odd number of arguments for Hash (ArgumentError)

Cheers

Jeremy Evans

unread,
Aug 28, 2021, 9:41:51 PMAug 28
to seque...@googlegroups.com
On Sat, Aug 28, 2021 at 2:26 PM bee...@gmail.com <bee...@gmail.com> wrote:
Not sure why this is happening.  I'm getting <subject> as an error.  I've tried using both directive and non-directive for @id in the initialize method, with same error.  Seems the object is the same column profile.  Any clue why this is happening?


DL.create_table? :mobibase do
  primary_key   :id

  DateTime      :cdate
  String        :label, size: 200
  String        :note, size: 350

  String        :status, size: 20, default: 'new'
end

class Mobibase < Sequel::Model(DL[:mobibase])

  def initialize(cdate, label, note)
#     @id = nil
    @cdate = cdate
    @label = label
    @note = note
    @status = 'new'
    puts "A new instantiation"
  end

Don't override #initialize for Sequel::Model subclasses.  Sequel::Model#initialize is expected to be called with a single hash.  In general, you should not override methods that Sequel::Model defines to change the number or type of arguments.  This is true not just for Sequel::Model, but for pretty much all Ruby classes, unless you are subclassing from Object or BasicObject.

Thanks,
Jeremy
Message has been deleted

bee...@gmail.com

unread,
Aug 28, 2021, 11:24:39 PMAug 28
to sequel-talk
Sorry...half way through a Saturday evening.  I will try with a single hash thrown, but how do I require such a structure, given I can't manipulate the initialize?

On Saturday, August 28, 2021 at 11:22:12 PM UTC-4 bee...@gmail.com wrote:
OK, understood.  How do I insist on required parameters, named or otherwise?

Marcelo Pereira

unread,
Aug 29, 2021, 6:54:43 AMAug 29
to seque...@googlegroups.com
If you need to, you can call super in your initialize, passing the parameters the way Sequel models expect.

--
You received this message because you are subscribed to the Google Groups "sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sequel-talk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/ccc17503-769c-48b2-8f23-0239770a6c28n%40googlegroups.com.

Jeremy Evans

unread,
Aug 29, 2021, 11:24:57 AMAug 29
to seque...@googlegroups.com
On Sat, Aug 28, 2021 at 8:22 PM bee...@gmail.com <bee...@gmail.com> wrote:
OK, understood.  How do I insist on required parameters, named or otherwise?

Probably easiest to check for values after calling super:

  def initialize(values={})
    super
    raise ArgumentError unless some_criteria
    # ...
  end
 
Note that this approach makes little sense in Sequel.  In Sequel, you can do:

 Model.new(values).save

but you can also do:

  Model.new.set(values).save

So checking for values in initialize does not make sense.  Sequel::Model expects you to override validate and call super:

  def validate
    super
    errors.add(:column1, "doesn't meet criteria") unless some_criteria
  end

validate is called before saving the model.

There are numerous plugins that offer additional validation methods and approaches.

Thanks,
Jeremy

bee...@gmail.com

unread,
Aug 29, 2021, 5:53:14 PMAug 29
to sequel-talk
Thank you
Reply all
Reply to author
Forward
0 new messages