All Projects → fnando → Ar Uuid

fnando / Ar Uuid

Licence: mit
Override migration methods to support UUID columns without having to be explicit about it.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Ar Uuid

Activerecord Postgres enum
Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
Stars: ✭ 227 (+453.66%)
Mutual labels:  activerecord, postgresql, postgres, rails
Activerecord Clean Db Structure
Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)
Stars: ✭ 101 (+146.34%)
Mutual labels:  activerecord, postgresql, postgres, rails
Pg party
ActiveRecord PostgreSQL Partitioning
Stars: ✭ 294 (+617.07%)
Mutual labels:  activerecord, postgresql, postgres, rails
Torque Postgresql
Add support to complex resources of PostgreSQL, like data types, array associations, and auxiliary statements (CTE)
Stars: ✭ 130 (+217.07%)
Mutual labels:  activerecord, postgresql, rails
Logidze
Database changes log for Rails
Stars: ✭ 1,060 (+2485.37%)
Mutual labels:  activerecord, postgresql, rails
Monogamy
Add table-level database locking to ActiveRecord
Stars: ✭ 12 (-70.73%)
Mutual labels:  activerecord, postgresql, postgres
Calculate All
calculate_all method for aggregate functions in Active Record
Stars: ✭ 118 (+187.8%)
Mutual labels:  activerecord, postgres, rails
Scenic
Scenic is maintained by Derek Prior, Caleb Hearth, and you, our contributors.
Stars: ✭ 2,856 (+6865.85%)
Mutual labels:  activerecord, postgres, rails
Niklick
Rails Versioned API solution template for hipsters! (Ruby, Ruby on Rails, REST API, GraphQL, Docker, RSpec, Devise, Postgress DB)
Stars: ✭ 39 (-4.88%)
Mutual labels:  postgresql, postgres, rails
Zero downtime migrations
Zero downtime migrations with ActiveRecord 3+ and PostgreSQL
Stars: ✭ 513 (+1151.22%)
Mutual labels:  activerecord, postgres, rails
Rails Pg Extras
Rails PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Stars: ✭ 432 (+953.66%)
Mutual labels:  activerecord, postgresql, rails
Mobility
Pluggable Ruby translation framework
Stars: ✭ 644 (+1470.73%)
Mutual labels:  activerecord, postgresql, rails
Activerecord Postgis Adapter
ActiveRecord connection adapter for PostGIS, based on postgresql and rgeo
Stars: ✭ 746 (+1719.51%)
Mutual labels:  activerecord, postgresql, rails
With advisory lock
Advisory locking for ActiveRecord
Stars: ✭ 409 (+897.56%)
Mutual labels:  activerecord, postgresql, postgres
Rein
Database constraints made easy for ActiveRecord.
Stars: ✭ 657 (+1502.44%)
Mutual labels:  activerecord, postgres, rails
Activerecordextended
Adds additional postgres functionality to an ActiveRecord / Rails application
Stars: ✭ 830 (+1924.39%)
Mutual labels:  activerecord, postgres, rails
Chinese regions rails
中国省市区县数据库,包含行政编码,邮政编码,地区拼音和简拼
Stars: ✭ 38 (-7.32%)
Mutual labels:  activerecord, rails
Ridgepole
Ridgepole is a tool to manage DB schema. It defines DB schema using Rails DSL, and updates DB schema according to DSL. (like Chef/Puppet)
Stars: ✭ 840 (+1948.78%)
Mutual labels:  postgresql, rails
Go Kallax
Kallax is a PostgreSQL typesafe ORM for the Go language.
Stars: ✭ 853 (+1980.49%)
Mutual labels:  postgresql, postgres
Node Pg Migrate
Node.js database migration management for Postgresql
Stars: ✭ 838 (+1943.9%)
Mutual labels:  postgresql, postgres

ar-uuid

Tests Code Climate Gem Gem

Override migration methods to support UUID columns without having to be explicit about it.

Installation

gem install ar-uuid

Or add the following line to your project's Gemfile:

gem "ar-uuid"

Usage

There's no setup. Just adding the gem to your Gemfile is enough. When you create a new table, the id column will be defined as uuid. This is also true for references.

create_table :users
add_reference :posts, :users

create_table :posts do |t|
  t.belongs_to :user
  # or
  t.references :user
end

If you need a serial column, AR's PostgreSQL supports the bigserial column type.

create_table :users do |t|
  t.column :position, :bigserial, null: false
end

Sorting

Rails 6.0 or newer

If you're using Rails 6.0 or newer, you can set a default sorting with ActiveRecord::ModelSchema.implicit_order_column, so methods like ActiveRecord::FinderMethods::InstanceMethods#first and ActiveRecord::FinderMethods::InstanceMethods#last will work transparently, as long as you define another column for sorting, such as created_at (you may need to add an index).

The following example sets a default behavior to always sort using created_at (when available). On your abstract model, add the following lines:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  def self.inherited(child_class)
    super

    return unless child_class.columns.any? {|col| col.name == "created_at" }

    child_class.implicit_order_column ||= "created_at"
  end
end

Older Rails versions

For older Rails versions, you can't use methods like ActiveRecord::FinderMethods::InstanceMethods#first and ActiveRecord::FinderMethods::InstanceMethods#last, since they are scoped to the sequential id.

The easiest alternative is ordering results and calling first/last. You can either create a sequence, or use the created_at/updated_at columns:

# Get first record
User.order(created_at: :asc).first

# Get last record
User.order(created_at: :desc).first

# Use scopes
class User < ApplicationRecord
  scope :newer, -> { order(created_at: :desc) }
  scope :older, -> { order(created_at: :asc) }
end

User.older.first
User.newer.first

You can also replace .first with ActiveRecord::FinderMethods::InstanceMethods#take, which will use the order implemented by the database.

There's no alternative to .last.

Maintainer

Contributors

Contributing

For more details about how to contribute, please read https://github.com/fnando/ar-uuid/blob/main/CONTRIBUTING.md.

License

The gem is available as open source under the terms of the MIT License. A copy of the license can be found at https://github.com/fnando/ar-uuid/blob/main/LICENSE.md.

Code of Conduct

Everyone interacting in the ar-uuid project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].