# Set a default value to a "text/blob" field at creation?

**URL:** <https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763>\
**Category:** Rails\
**Created:** [April 3, 2008, 11:15am UTC](https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763 "2008-04-03T11:15:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [April 3, 2008, 11:15am UTC](https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763/1 "2008-04-03T11:15:59Z")

</div>

Is it possible somehow to incorporate “this is a text” into a  
“blob/text”-value in the database when the row is created?  
It’s not possible to do with a default value hence it’s a  
“blob/text”-value.

At the moment I create the row for a user with:

@presentation = current\_user.presentation  
@presentation = current\_user.create\_presentation unless @presentation

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [April 3, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763/2 "2008-04-03T14:40:40Z")

</div>

I tried to create this in my lib file string.rb to show something else  
if the value was nil…(and required the ‘string.rb’ in my application  
helper)

def or\_empty(alternate)  
(self.nil? or self == “”) ? alternate : self  
end

but that didn’t work either.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [April 4, 2008, 11:08am UTC](https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763/3 "2008-04-04T11:08:32Z")

</div>

Julian L. wrote:

> You’d need to open the class up to do this.
> 
> class String  
> def or\_empty(alternate = ‘default’)  
> self.blank? ? alternate : self  
> end
> 
> That would work.
> 
> Julian.
> 
> Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) VIDEO #3  
> OUT APRIL 6  
> [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

Nope, that doesn’t do it,

I still get an error  
“The error occurred while evaluating nil.or\_empty”

in this line of my show-view:  
\<%= h @presentation.pres.or\_empty(“No presentation has been entered”)%\>

because the there exists nothing in the pres-field. This is what I’m  
trying to fix.

what do you mean with:

> You’d need to open the class up to do this.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [April 4, 2008, 2:13am UTC](https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763/4 "2008-04-04T02:13:51Z")

</div>

You’d need to open the class up to do this.

class String  
def or\_empty(alternate = ‘default’)  
self.blank? ? alternate : self  
end

That would work.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) VIDEO #3  
OUT APRIL 6  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [April 4, 2008, 11:47am UTC](https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763/5 "2008-04-04T11:47:08Z")

</div>

Frederick C. wrote:

> On 4 Apr 2008, at 10:08, Dag S. wrote:
> 
> > because the there exists nothing in the pres-field. This is what I’m  
> > trying to fix.
> 
> How about you do this in your model
> 
> def pres  
> value\_in\_database = self[:pres]  
> #do something with value\_in\_database  
> end
> 
> Fred

I’m not quite sure I completely understand… =/

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [April 4, 2008, 11:59am UTC](https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763/6 "2008-04-04T11:59:16Z")

</div>

On 4 Apr 2008, at 10:47, Dag S. wrote:

> > #do something with value\_in\_database  
> > end
> > 
> > Fred
> 
> I’m not quite sure I completely understand… =/

Overwrite the accessor that rails provides, using self[:pres] to get  
at the original value.

Fred

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [April 4, 2008, 11:21am UTC](https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763/7 "2008-04-04T11:21:52Z")

</div>

On 4 Apr 2008, at 10:08, Dag S. wrote:

> because the there exists nothing in the pres-field. This is what I’m  
> trying to fix.

How about you do this in your model

def pres  
value\_in\_database = self[:pres]  
#do something with value\_in\_database  
end

Fred

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [April 4, 2008, 1:19pm UTC](https://www.ruby-forum.com/t/set-a-default-value-to-a-text-blob-field-at-creation/133763/8 "2008-04-04T13:19:28Z")

</div>

Frederick C. wrote:

> On 4 Apr 2008, at 10:47, Dag S. wrote:
> 
> > > #do something with value\_in\_database  
> > > end
> > > 
> > > Fred
> > 
> > I’m not quite sure I completely understand… =/
> 
> Overwrite the accessor that rails provides, using self[:pres] to get  
> at the original value.
> 
> Fred

Alright, thanx a lot, solved the problem!
