views:

46

answers:

1

I have an action that saves new procucts like this:

@products = Product.new(params[:product]) 
@products.save

I am calling the same action for editing a product as well. But each time I edit...a new product gets created (because of above statements).

A while back I had seen some function that will only create a new record if there isn't an exising record. create_or_update something like that. I can not find it now.

Can anyone tell me what the method was? or give me a better approach to do this?

+1  A: 

I think you're looking for find_or_create

You can see the docs for it at http://railsapi.com/doc/rails-v2.3.5/classes/ActiveRecord/Base.html

Dan McNevin