# KirbyBase, Table Default Values not being assigned

**URL:** <https://www.ruby-forum.com/t/kirbybase-table-default-values-not-being-assigned/153341>\
**Category:** Ruby\
**Created:** [November 15, 2008, 5:04am UTC](https://www.ruby-forum.com/t/kirbybase-table-default-values-not-being-assigned/153341 "2008-11-15T05:04:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [November 15, 2008, 5:04am UTC](https://www.ruby-forum.com/t/kirbybase-table-default-values-not-being-assigned/153341/1 "2008-11-15T05:04:48Z")

</div>

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???

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [November 18, 2008, 8:02am UTC](https://www.ruby-forum.com/t/kirbybase-table-default-values-not-being-assigned/153341/2 "2008-11-18T08:02:59Z")

</div>

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???

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [November 18, 2008, 9:27am UTC](https://www.ruby-forum.com/t/kirbybase-table-default-values-not-being-assigned/153341/3 "2008-11-18T09:27:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [November 25, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/kirbybase-table-default-values-not-being-assigned/153341/4 "2008-11-25T12:48:05Z")

</div>

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