{"id":13879474,"url":"https://github.com/fnando/ar-uuid","last_synced_at":"2025-04-17T00:51:34.796Z","repository":{"id":31114919,"uuid":"34674409","full_name":"fnando/ar-uuid","owner":"fnando","description":"Override migration methods to support UUID columns without having to be explicit about it.","archived":false,"fork":false,"pushed_at":"2023-03-09T19:18:16.000Z","size":77,"stargazers_count":46,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T05:51:11.133Z","etag":null,"topics":["activerecord","postgres","postgresql","primary-key","rails","uuid"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fnando.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["fnando"],"custom":["https://paypal.me/nandovieira/🍕"]}},"created_at":"2015-04-27T15:22:25.000Z","updated_at":"2022-09-29T01:00:53.000Z","dependencies_parsed_at":"2024-01-13T20:57:17.820Z","dependency_job_id":"c6f31bc2-d4a1-4423-97fc-62b521a240b6","html_url":"https://github.com/fnando/ar-uuid","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Far-uuid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Far-uuid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Far-uuid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Far-uuid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fnando","download_url":"https://codeload.github.com/fnando/ar-uuid/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249294896,"owners_count":21246009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","postgres","postgresql","primary-key","rails","uuid"],"created_at":"2024-08-06T08:02:22.186Z","updated_at":"2025-04-17T00:51:34.771Z","avatar_url":"https://github.com/fnando.png","language":"Ruby","funding_links":["https://github.com/sponsors/fnando","https://paypal.me/nandovieira/🍕"],"categories":["Ruby"],"sub_categories":[],"readme":"# ar-uuid\n\n[![Tests](https://github.com/fnando/ar-uuid/workflows/ruby-tests/badge.svg)](https://github.com/fnando/ar-uuid)\n[![Gem](https://img.shields.io/gem/v/ar-uuid.svg)](https://rubygems.org/gems/ar-uuid)\n[![Gem](https://img.shields.io/gem/dt/ar-uuid.svg)](https://rubygems.org/gems/ar-uuid)\n\nOverride migration methods to support UUID columns without having to be explicit\nabout it.\n\n## Installation\n\n```bash\ngem install ar-uuid\n```\n\nOr add the following line to your project's Gemfile:\n\n```ruby\ngem \"ar-uuid\"\n```\n\n## Usage\n\nThere's no setup. Just adding the gem to your Gemfile is enough. When you create\na new table, the `id` column will be defined as `uuid`. This is also true for\nreferences.\n\n```ruby\ncreate_table :users\nadd_reference :posts, :users\n\ncreate_table :posts do |t|\n  t.belongs_to :user\n  # or\n  t.references :user\nend\n```\n\nIf you need a serial column, AR's PostgreSQL supports the `bigserial` column\ntype.\n\n```ruby\ncreate_table :users do |t|\n  t.column :position, :bigserial, null: false\nend\n```\n\n### Sorting\n\n#### Rails 6.0 or newer\n\nIf you're using Rails 6.0 or newer, you can set a default sorting with\n[ActiveRecord::ModelSchema.implicit_order_column](https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema.html#method-c-implicit_order_column),\nso methods like `ActiveRecord::FinderMethods::InstanceMethods#first` and\n`ActiveRecord::FinderMethods::InstanceMethods#last` will work transparently, as\nlong as you define another column for sorting, such as `created_at` (you may\nneed to add an index).\n\nThe following example sets a default behavior to always sort using `created_at`\n(when available). On your abstract model, add the following lines:\n\n```ruby\nclass ApplicationRecord \u003c ActiveRecord::Base\n  self.abstract_class = true\n\n  def self.inherited(child_class)\n    super\n\n    return unless child_class.columns.any? {|col| col.name == \"created_at\" }\n\n    child_class.implicit_order_column ||= \"created_at\"\n  end\nend\n```\n\n#### Older Rails versions\n\nFor older Rails versions, you can't use methods like\n`ActiveRecord::FinderMethods::InstanceMethods#first` and\n`ActiveRecord::FinderMethods::InstanceMethods#last`, since they are scoped to\nthe sequential id.\n\nThe easiest alternative is ordering results and calling `first`/`last`. You can\neither create a sequence, or use the `created_at`/`updated_at` columns:\n\n```ruby\n# Get first record\nUser.order(created_at: :asc).first\n\n# Get last record\nUser.order(created_at: :desc).first\n\n# Use scopes\nclass User \u003c ApplicationRecord\n  scope :newer, -\u003e { order(created_at: :desc) }\n  scope :older, -\u003e { order(created_at: :asc) }\nend\n\nUser.older.first\nUser.newer.first\n```\n\nYou can also replace `.first` with\n[ActiveRecord::FinderMethods::InstanceMethods#take](https://github.com/rails/rails/blob/f52354ad1d15120dcc5284714bee7ee3f052986c/activerecord/lib/active_record/relation/finder_methods.rb#L104),\nwhich will use the order implemented by the database.\n\nThere's no alternative to `.last`.\n\n## Maintainer\n\n- [Nando Vieira](https://github.com/fnando)\n\n## Contributors\n\n- https://github.com/fnando/ar-uuid/contributors\n\n## Contributing\n\nFor more details about how to contribute, please read\nhttps://github.com/fnando/ar-uuid/blob/main/CONTRIBUTING.md.\n\n## License\n\nThe gem is available as open source under the terms of the\n[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be\nfound at https://github.com/fnando/ar-uuid/blob/main/LICENSE.md.\n\n## Code of Conduct\n\nEveryone interacting in the ar-uuid project's codebases, issue trackers, chat\nrooms and mailing lists is expected to follow the\n[code of conduct](https://github.com/fnando/ar-uuid/blob/main/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Far-uuid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffnando%2Far-uuid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Far-uuid/lists"}