# Optimizing Rails Code

**URL:** <https://www.ruby-forum.com/t/optimizing-rails-code/109663>\
**Category:** Rails\
**Created:** [July 17, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/optimizing-rails-code/109663 "2007-07-17T17:56:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 17, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/optimizing-rails-code/109663/1 "2007-07-17T17:56:41Z")

</div>

Hello,

I often find myself creating and retrieving ActiveRecord objects that  
barely get used, unaware of perhaps a better way of database  
interaction within Rails. Are there more efficient ways to do some of  
the following things:

Ex 1:  
Supplier.find(supplierid).update\_attributes(:num\_products =\>  
total, :ship\_fee =\> SHIPPING\_FEE)

Ex 2:  
item = Product.find(  
:first,  
:conditions =\> [“supplierid = #{supplierid} AND sku =  
‘#{attributes[:sku]}’”]  
)  
attributes[:last\_change] = Date.today if product\_changed(item,  
attributes)  
attributes[:last\_update] = Date.today  
item.update\_attributes(attributes)

Ex 3:  
image = SupplierImage.new(  
:productid =\> productid,  
:mid =\> IMAGE\_MACHINE,  
:filename =\> filename,  
:alt =\> prodname,  
:supplier\_url =\> url  
)  
image.save

Thanks!

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 17, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/optimizing-rails-code/109663/2 "2007-07-17T18:29:45Z")

</div>

In the third instance, you could just use “create” instead of  
“new/save”. Doesn’t save you many lines (1) and doesn’t really make  
it any more efficient (all that does is call new/save), but hey at  
least it looks better. 🙂

–Jeremy

On 7/17/07, Matt W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> total, :ship\_fee =\> SHIPPING\_FEE)  
> item.update\_attributes(attributes)
> 
> Thanks!
> 
> >

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 17, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/optimizing-rails-code/109663/3 "2007-07-17T19:31:56Z")

</div>

> Ex 1:  
> Supplier.find(supplierid).update\_attributes(:num\_products =\>  
> total, :ship\_fee =\> SHIPPING\_FEE)

Supplier.update(supplierid, :num\_products =\> total, :ship\_fee =\>  
SHIPPING\_FEE)

> 

Not clear about this one.

## -Pratik

[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 17, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/optimizing-rails-code/109663/4 "2007-07-17T23:52:39Z")

</div>

Matt W. wrote:

> Ex 2:  
> item = Product.find(  
> :first,  
> :conditions =\> [“supplierid = #{supplierid} AND sku =  
> ‘#{attributes[:sku]}’”]  
> )

You are leaving yourself open to SQL injection here. You should always  
use :conditions =\> [‘supplierid = ? AND sku = ?’, supplierid,  
attributes[:sku]]

Also, you don’t need to do a find if all you want to do is to update an  
attribute, checkout update\_all for that.

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 18, 2007, 6:28am UTC](https://www.ruby-forum.com/t/optimizing-rails-code/109663/5 "2007-07-18T06:28:39Z")

</div>

On 7/17/07, Pratik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Ex 1:  
> > Supplier.find(supplierid).update\_attributes(:num\_products =\>  
> > total, :ship\_fee =\> SHIPPING\_FEE)
> 
> Supplier.update(supplierid, :num\_products =\> total, :ship\_fee =\> SHIPPING\_FEE)

This saves you a line of code. If all you want to do is update some  
fields quickly, use update\_all, though it’s closer to raw sql.

Supplier.update\_all [‘num\_products = ?, ship\_fee = ?’, total,  
SHIPPING], [‘id=?’, supplierid]

Though no validations or callbacks are called on your model.

> > Ex 2:  
> > item = Product.find(  
> > :first,  
> > :conditions =\> [“supplierid = #{supplierid} AND sku =  
> > ‘#{attributes[:sku]}’”]  
> > )  
> > attributes[:last\_change] = Date.today if product\_changed(item,  
> > attributes)  
> > attributes[:last\_update] = Date.today  
> > item.update\_attributes(attributes)

Same thing, all you’re doing is updating a few timestamps on a record.

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