diff --git a/README.md b/README.md index b5992b5..ea9614f 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ orders.insert_all \ ```ruby # db/seeds/data/plans.rb -plans.insert :basic, title: "Basic", price_cents: 10_00 +plans.upsert :basic, title: "Basic", price_cents: 10_00 ``` Seed files will generally use `create` and/or `insert`. Passing a symbol to name the record is useful when reusing the data in tests. diff --git a/lib/oaken/stored/active_record.rb b/lib/oaken/stored/active_record.rb index e1388f6..ccc5fd2 100644 --- a/lib/oaken/stored/active_record.rb +++ b/lib/oaken/stored/active_record.rb @@ -20,12 +20,12 @@ def create(label = nil, **attributes) record end - def insert(label = nil, **attributes) + def upsert(label = nil, **attributes) attributes = @attributes.merge(attributes) attributes.transform_values! { _1.respond_to?(:call) ? _1.call : _1 } type.new(attributes).validate! - record = type.new(id: type.insert(attributes, returning: :id).rows.first.first) + record = type.new(id: type.upsert(attributes, returning: :id).rows.first.first) label label => record if label record end diff --git a/test/dummy/db/seeds/data/plans.rb b/test/dummy/db/seeds/data/plans.rb index 6f3b4bc..16bd62d 100644 --- a/test/dummy/db/seeds/data/plans.rb +++ b/test/dummy/db/seeds/data/plans.rb @@ -1 +1 @@ -plans.insert :basic, title: "Basic", price_cents: 10_00 +plans.upsert :basic, title: "Basic", price_cents: 10_00 diff --git a/test/dummy/db/seeds/test/data/plans.rb b/test/dummy/db/seeds/test/data/plans.rb index 3c0742a..c186268 100644 --- a/test/dummy/db/seeds/test/data/plans.rb +++ b/test/dummy/db/seeds/test/data/plans.rb @@ -1 +1 @@ -plans.insert :test_premium, title: "Premium", price_cents: 20_00 +plans.upsert :test_premium, title: "Premium", price_cents: 20_00