rails 3.1 active record insert or update
问题 I'm new to rails. Is there an easy way in active record to pass it a hash of data and if the record exists, update it, and if it doesn't, create it? data = {} data["my_id"] = 356345 data["description"] = "test123" w = Descriptions.new(data) Ideally if I ran the above it would only ever have 1 record, not multiple records each time I ran it. 回答1: Assuming you ware wanting the "my_id" bit to be unique you can run Descriptions.find_or_create_by_my_id(data["my_id"]).update_attributes(data) 回答2: