# Update dynamic fields automatically in database

**URL:** <https://www.ruby-forum.com/t/update-dynamic-fields-automatically-in-database/112068>\
**Category:** Rails\
**Created:** [August 14, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/update-dynamic-fields-automatically-in-database/112068 "2007-08-14T22:27:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaystotz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jaystotz](https://www.ruby-forum.com/u/jaystotz)\
**Post date:** [August 14, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/update-dynamic-fields-automatically-in-database/112068/1 "2007-08-14T22:27:05Z")

</div>

I’m working on a shopping cart application and I’ve run into a design  
question:

I have a field in the cart\_lines table called total\_price. That field  
is really just a calculated field based on qty and unit\_price. If  
either of those values change, total\_price should also be updated.

The way that makes the most sense to me is to write a method called  
total\_price that defines how it should be calculated rather than  
modify the value directly from both the qty= and unit\_price= methods.  
The problem is, I want that value stamped in the database so I can use  
it as a condition or sort field in queries.

I’ve written the following code (which works fine) that loops through  
the object’s attributes, checks to see if a method exists with the  
same name, and updates the value if it’s different:

class CartLine \< ActiveRecord::Base  
before\_save :recalculate

def total\_price  
qty \* unit\_price  
end

def recalculate  
attributes.each do |attribute\_name, attribute\_value|  
if respond\_to\_without\_attributes? attribute\_name  
method\_value = send(attribute\_name)  
send(attribute\_name + “=”, method\_value) if method\_value !=  
attribute\_value  
end  
end  
end  
end

That’s fine and it works well but I’m wondering if I’m missing  
something or there’s a simpler way to handle this since I’m going to  
have to reuse that code all over the place. If not, I could just  
refactor it to a plugin but I’d be surprised since it sounds like it  
would be a common situation.

How would you handle this? Any ideas or insight would be appreciated.  
Thanks!

---

<div class="post-metadata">

**Author:** ![jaystotz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jaystotz](https://www.ruby-forum.com/u/jaystotz)\
**Post date:** [August 14, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/update-dynamic-fields-automatically-in-database/112068/2 "2007-08-14T23:02:09Z")

</div>

jaystotz wrote:

> I’ve written the following code (which works fine) that loops through  
> the object’s attributes, checks to see if a method exists with the  
> same name, and updates the value if it’s different:

I’m wondering if you need all of that. When you save a record in Rails  
all the attributes are put in the update statement (not just the ones  
changed). So can’t you just define total\_price how you have and since  
total\_price is a real field in the database it should get qty \*  
unit\_price without any callback. I think… I haven’t tried it myself.  
🙂

Eric

---

<div class="post-metadata">

**Author:** ![jaystotz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jaystotz](https://www.ruby-forum.com/u/jaystotz)\
**Post date:** [August 15, 2007, 6:17am UTC](https://www.ruby-forum.com/t/update-dynamic-fields-automatically-in-database/112068/3 "2007-08-15T06:17:34Z")

</div>

You know, I assumed the same thing which led me to this question. But  
it turns out the values used in the update statement are based on the  
values in the internal @attributes hash, not the results of method  
calls. Defining a method named total\_price does not affect the value  
used in the update statement.
