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)
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.