# DB trigger vs before\_create

**URL:** <https://www.ruby-forum.com/t/db-trigger-vs-before-create/103436>\
**Category:** Rails\
**Created:** [May 6, 2007, 12:55am UTC](https://www.ruby-forum.com/t/db-trigger-vs-before-create/103436 "2007-05-06T00:55:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 6, 2007, 12:55am UTC](https://www.ruby-forum.com/t/db-trigger-vs-before-create/103436/1 "2007-05-06T00:55:42Z")

</div>

Hello everyone,

I’m having trouble figuring out which approach is better.  
I have the following tables: products, orders and line\_items.  
Every time an Order is placed, the quantity field in the products  
table has to be updated for each LineItem in the Order. One option is  
to use DB triggers which is not as portable as using before\_create in  
the Order model. But with before\_create I cannot use fixtures for  
orders and line\_items since they don’t use before\_create as models do.

Has anyone any recommendations on which approach is better?

Erik

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 6, 2007, 1:46am UTC](https://www.ruby-forum.com/t/db-trigger-vs-before-create/103436/2 "2007-05-06T01:46:45Z")

</div>

> But with before\_create I cannot use fixtures for  
> orders and line\_items since they don’t use before\_create as models do.

Er… What?

The standard rails practice is to put the logic in your models. I  
find code like this is easier to write, maintain, and test.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 6, 2007, 2:14am UTC](https://www.ruby-forum.com/t/db-trigger-vs-before-create/103436/3 "2007-05-06T02:14:47Z")

</div>

Yes, but the standard Rails practice is also to use fixtures for  
testing, but putting that logic into models means no fixtures.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 6, 2007, 6:47am UTC](https://www.ruby-forum.com/t/db-trigger-vs-before-create/103436/4 "2007-05-06T06:47:01Z")

</div>

Not quite… fixtures are “fixed” pieces of data that you test your  
models and controllers against. You need to make sure they always fit  
your business rules even as they change.

Fixtures are not meant for testing your validation methods. You  
should use unit tests for that.
