Given the code below, how can default values be defined for the Model. (let's say the default for :name should be 'Thing').
require 'pp'
require 'sequel'
DB = Sequel.sqlite
DB.create_table :items do
primary_key :id
String :name
end
items = DB[ :items ]
class Item < Sequel::Model
end
Item.create :name => 'foobar'
Item.create
pp Item.all
# =>
# >> [#<Item @values={:name=>"foobar", :id=>1}>,
# >> #<Item @values={:name=>nil, :id=>2}>]
So, I'd like to have the second created Item set to #<Item @values={:name=>"Thing", :id=>2}> rather than :name=>nil.
after_create
?after_initialize
seems to be a plugin hook that is "called for all model instances on creation (both new instances and instances retrieved from the database). It exists mostly for legacy compatibility, but it is still supported." See sequel.jeremyevans.net/rdoc/files/doc/model_hooks_rdoc.html – Caeoma