# Opinion on default values -- DB or AR?

**URL:** <https://www.ruby-forum.com/t/opinion-on-default-values-db-or-ar/116968>\
**Category:** Rails\
**Created:** [October 11, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/opinion-on-default-values-db-or-ar/116968 "2007-10-11T18:59:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 11, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/opinion-on-default-values-db-or-ar/116968/1 "2007-10-11T18:59:30Z")

</div>

I was working yesterday and realized that I couldn’t find how a model of  
mine was getting a default value:

part = Part.new  
part.type # 1

I scoured my model and its observer, but such a default had not been  
programmed. Then I checked the schema, and there it was: :default =\> 1.

So what’s your take? How do you feel about defining defaults in the  
database? If you define a default in the database should you validate  
the attribute anyway? Should you double-enforce the default in your  
model? What do you do?

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 11, 2007, 7:16pm UTC](https://www.ruby-forum.com/t/opinion-on-default-values-db-or-ar/116968/2 "2007-10-11T19:16:37Z")

</div>

> database? If you define a default in the database should you validate  
> the attribute anyway? Should you double-enforce the default in your  
> model? What do you do?

Do it in the database. That way in a year when you run some SQL  
manually  
via some other means “just this one time” you won’t shoot yourself in  
the  
foot.

Wouldn’t hurt to clarify it in the model as well though or make a note  
of  
it perhaps so a year from now you remember where that’s coming from…

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 11, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/opinion-on-default-values-db-or-ar/116968/3 "2007-10-11T19:31:04Z")

</div>

Philip H. wrote:

> > database? If you define a default in the database should you validate  
> > the attribute anyway? Should you double-enforce the default in your  
> > model? What do you do?
> 
> Do it in the database. That way in a year when you run some SQL  
> manually via some other means “just this one time” you won’t  
> shoot yourself in the foot.

That’s a fair point.

In this particular situation though the value being defaulted is a  
foreign key. It’s a default, but with an assumption that there’s a  
record in another table with an ID of 1. So it feels like that breaks  
any kind of safety it tried to create.

I guess I can agree with defaults that aren’t foreign keys and are  
documented along with the class.
