diff --git a/README.md b/README.md index 5ba2bf7..ba02069 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,7 @@ _305 TILs and counting..._ - [Rake Only Lists Tasks With Descriptions](ruby/rake-only-lists-tasks-with-descriptions.md) - [Replace The Current Process With An External Command](ruby/replace-the-current-process-with-an-external-command.md) - [Rendering ERB](ruby/rendering-erb.md) +- [Returning With Sequel](ruby/returning-with-sequel.md) - [Safe Navigation Operator](ruby/safe-navigation-operator.md) - [Set RVM Default Ruby](ruby/set-rvm-default-ruby.md) - [Squeeze Out The Extra Space](ruby/squeeze-out-the-extra-space.md) diff --git a/ruby/returning-with-sequel.md b/ruby/returning-with-sequel.md new file mode 100644 index 0000000..81a9454 --- /dev/null +++ b/ruby/returning-with-sequel.md @@ -0,0 +1,21 @@ +# Returning With Sequel + +The [`sequel`](https://github.com/jeremyevans/sequel) gem is a database +toolkit that allows you to interact with most databases. PostgreSQL has +support for composite primary keys, but `sequel`, which is supposed to return +the `id` of newly inserted records, isn't sure what to return when faced +with a composite primary key. You can get around this by telling `sequel` +exactly what should be returned using the `#returning` method. For instance, +get it to return just the `id` of the new record: + +```ruby +DB[:floors].returning(:id).insert(hotel_id: 4, id: 1, ...) +# [{id: 1}] +``` + +To get it to return both parts of composite key: + +```ruby +DB[:floors].returning(:id, :hotel_id).insert(hotel_id: 4, id: 1, ...) +# [{id: 1, hotel_id: 4}] +```