KirbyBase, Table Default Values not being assigned

Discussion in 'Ruby' started by Adam Akhtar, Nov 15, 2008.

  1. Adam Akhtar

    Adam Akhtar Guest

    Hi Ive been using KirbyBase and got it up and working and been fine with
    basic stuff such as inserting and selecting. I create my own record
    class as well to retrieve and pass data to the table.

    The problem is with using default values. I cant seem to get the db to
    substitiute any blank (nil) values in my custom record class with the
    default values I specified when creating the table. When i check the
    table in a text editor after an insert the values are kb_nil - hence its
    not substituting like it should during the insert operation. Hence when
    I do a select on the table in question i get nil values back rather than
    the default values.

    I experimented in irb and when i dont create a custom record class it
    seems to work but i cant find anything in the docs that suggest i should
    be doing anything different if i use my own custom record class.

    heres is some code for you to use in irb which makes use of a custom
    record class called task (im making a todo list) which replicates the
    problem


    require 'kirbybase'

    class Task
    attr_accessor:)recno, :title)

    def Task.kb_create(recno, title)
    Task.new do |x|
    x.recno = recno
    x.title = title
    end
    end

    def initialize(&block)
    instance_eval(&block)
    end
    end

    db = KirbyBase.new
    tbl = db.create_table:)deleteme, :title, {:DataType=>:String,
    :Default=>'No Title'}) do |t|
    t.record_class = Task
    end

    a_task = Task.new {|r| r.title = nil}
    tbl.insert(a_task)
    tbl.select

    ============================================
    the result of the above select
    ---------------------------------------------
    irb(main):029:0> tbl.select
    => [#<Task:0x4ec71e8 @title=nil, @recno=1>]

    title should not be nil. It should be "No Title".
    finally

    here is the table file after insert

    000001|000000|Task|recno:Integer|title:String:Default->No Title
    1|kb_nil


    can anyone tell me where im going wrong on this????
    --
    Posted via http://www.ruby-forum.com/.
    Adam Akhtar, Nov 15, 2008
    #1
    1. Advertising

  2. Adam Akhtar

    Adam Akhtar Guest

    im still banging my head against a wall on this one. I have no idea what
    im doing wrong...are there any other kirbybase users out there who can
    test the code ive supplied and see if they get the same error???
    --
    Posted via http://www.ruby-forum.com/.
    Adam Akhtar, Nov 18, 2008
    #2
    1. Advertising

  3. Adam Akhtar

    Pit Capitain Guest

    Adam, from quickly browsing through the KirbyBase code, it looks like
    the normal default value mechanism doesn't work if you use custom
    record classes. A default value for a column is used only if the data
    passed to #insert doesn't contain a value for the column. An instance
    of your custom record class always has accessors for all columns, so
    the default values are never used. I think you have to handle default
    values in your custom class.

    Regards,
    Pit
    Pit Capitain, Nov 18, 2008
    #3
  4. Adam Akhtar

    Adam Akhtar Guest

    Thanks Pit for that, ive decided to scrap the class and just work with
    hashses and structs.

    --
    Posted via http://www.ruby-forum.com/.
    Adam Akhtar, Nov 25, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MU
    Replies:
    3
    Views:
    2,803
  2. Morris Neuman

    Default Values being displayed in DetailsView

    Morris Neuman, Dec 11, 2008, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    218
    Allen Chen [MSFT]
    Dec 16, 2008
  3. basi
    Replies:
    3
    Views:
    178
  4. basi
    Replies:
    2
    Views:
    99
  5. kie
    Replies:
    2
    Views:
    296
    W d'Anjos
    Oct 2, 2003
Loading...

Share This Page