How can default values in Sequel Models be set?
Asked Answered
A

3

8

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.

Atmosphere answered 19/7, 2009 at 8:44 Comment(0)
R
8

Subba's answer is the recommended way to do it in Sequel. It pushes the default into the database.

If you want to have defaults in the model instead of in the database, I recommend using a before_create or after_initialize hook to do them:

class Item < Sequel::Model
  def before_create # or after_initialize
    super
    self.name ||= 'Thing'
  end
end

The difference between after_initialize and before_create is when they are called. before_create is recommended, since it won't set the default until right before the database INSERT method is called. However, if you want:

Item.new.name == 'Thing'

then you have to use after_initialize.

Roving answered 4/8, 2009 at 21:30 Comment(2)
Do you mean 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.htmlCaeoma
Just note that after_initialize needs a plugin - sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/…Saharanpur
P
4
DB.create_table :items do
        primary_key :id
        String :name,:default => 'Thing'
end

should do the trick

from the sequel Sequel::Database source create_table block is evaluated inside Schema::Generator

def create_table(name, options={}, &block)
    options = {:generator=>options} if options.is_a?(Schema::Generator)
    generator = options[:generator] || Schema::Generator.new(self, &block)
    create_table_from_generator(name, generator, options)
    create_table_indexes_from_generator(name, generator, options)
end

inside Schema::Generator class method_missing handles String,text,boolean,number are handled by column method

def method_missing(type, name = nil, opts = {})
     name ? column(name, type, opts) : super
end

please refer to sequel column for additional options

Phototaxis answered 20/7, 2009 at 14:47 Comment(0)
J
2

I could set default values with using defaults_setter.

For all models. (Call this before defining subclasses that want to use default_setter)

Sequel::Model.plugin :defaults_setter

Only for particular model.

Item.plugin :defaults_setter

or

Item class << Sequel::Model
  plugin :defaults_setter
end

You can set default value like this.

Item.default_values[:name] = 'foobar'

Thank you.

Jacksonjacksonville answered 21/9, 2017 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.