How can I pass multiple attributes to find_or_create_by in Rails 3?

后端 未结 5 1144
独厮守ぢ
独厮守ぢ 2020-12-12 20:53

I want to use find_or_create_by, but this statement does NOT work. It does not \"find\" or \"create\" with the other attributes.

productproperty = ProductPro         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 21:09

    You could also use where(...).first_or_create - ActiveRecord::Relation#first_or_create.

    product_property_attrs  = { product_id: product.id, 
                                property_id: property.id, 
                                value: d[descname] }
    
    product_property = ProductProperty.where(product_property_attrs).first_or_create
    

提交回复
热议问题