if ClassName.exists?(["id = ?", self.id])
object = ClassName.find_by_name(self.name)
object.update_attributes!( :street_address => self.street_address,
:city_name => self.city_name,
:name => self.org_unit_name,
:state_prov_id => self.state_prov_id,
:zip_code => self.zip_code)
else
ClassName.create! :street_address => self.street_address,
:city_name => self.city_name,
:federalid => self.federalid,
:name => self.org_unit_name,
:state_prov_id => self.state_prov_id,
:zip_code => self.zip_code
end
I have code like this. I would like to improve it so that it uses a method, something like create_or_update.
ClassName.create_or_update_by_name(:name => self.name,
:street_address => self.street_address,
:city_name => self.city_name,
:federalid => self.federalid,
:name => self.org_unit_name,
:state_prov_id => self.state_prov_id,
:zip_code => self.zip_code)
If the name
exists in the database then it should update that object otherwise it should create a new object.
Is there is any method that exists that I can do this with?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…