开发者

how to create new or update if already existing product in rails

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? 开发者_Python百科or give me a better approach to do this?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜