# Update\_attribute updates too much

**URL:** <https://www.ruby-forum.com/t/update-attribute-updates-too-much/139422>\
**Category:** Rails\
**Created:** [June 3, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/update-attribute-updates-too-much/139422 "2008-06-03T18:56:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thorsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d2c977/32.png) [@thorsten](https://www.ruby-forum.com/u/thorsten)\
**Post date:** [June 3, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/update-attribute-updates-too-much/139422/1 "2008-06-03T18:56:51Z")

</div>

Hi all. Maybe somebody can explain that to me:

def create  
@orders = Order.find(:all, :conditions =\> [“products.member\_id = ?”,  
current\_login.member.id])

# new payment

@payment = Payment.new  
@payment.member = current\_login.member  
@payment.status = Payment::STATUS\_NEW  
@payment.save

sum = 0.00  
@orders.each do |order|  
order.payment = @payment  
order.save  
sum += 2950  
end  
logger.info “COUNT A: #{@payment.orders\_count}”  
@payment.update\_attribute(:total, sum)  
@payment.reload  
logger.info “COUNT B: #{@payment.orders\_count}”

works fine but for one point:  
The counter cache is 0 after that.

Reason is this.

While the line

order.payment = @payment

generates the necessary code to update the counter\_cache (twice):

UPDATE payments SET `orders_count` = `orders_count` + 1 WHERE (`id` =  
1017343603)

the update\_attribute line updates more than necessary:

UPDATE `payments` SET `created_at` = ‘2008-06-03 18:42:03’, `member_id`  
= 762526581, `status` = 1, `orders_count` = 0, `total` = 5900.0,  
`updated_at` = ‘2008-06-03 18:42:03’ WHERE `id` = 1017343603

Why is it updating not only the total and updated\_at, but in addition a  
whole set of different columns, that where not even changed since the  
@payment.save?

Am I doing something terribly wrong?  
I could handle this for the specific problem, but I see a lot of  
possible side effects here and would at least want to understand, what’s  
the reason for Rails to behave that way…

---

<div class="post-metadata">

**Author:** ![thorsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d2c977/32.png) [@thorsten](https://www.ruby-forum.com/u/thorsten)\
**Post date:** [June 3, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/update-attribute-updates-too-much/139422/2 "2008-06-03T18:59:41Z")

</div>

On 3 Jun 2008, at 17:56, Thorsten M. wrote:

> whole set of different columns, that where not even changed since the  
> @payment.save?

Just the way rails is. In 2.1 you can turn on partial\_updates

Fred

---

<div class="post-metadata">

**Author:** ![thorsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d2c977/32.png) [@thorsten](https://www.ruby-forum.com/u/thorsten)\
**Post date:** [June 4, 2008, 10:13am UTC](https://www.ruby-forum.com/t/update-attribute-updates-too-much/139422/3 "2008-06-04T10:13:56Z")

</div>

> Just the way rails is. In 2.1 you can turn on partial\_updates
> 
> Fred

Thanks Fred, so at least I know that I have to be a bit more careful  
with update\_attributes.  
I will give partial\_updates a try

thorsten
