Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Override migration methods to support UUID columns without having to be explicit about it.
gem install ar-uuid
Or add the following line to your project's Gemfile:
gem "ar-uuid"
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
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
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
.
For more details about how to contribute, please read https://github.com/fnando/ar-uuid/blob/main/CONTRIBUTING.md.
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.
Everyone interacting in the ar-uuid project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that ar-uuid demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.