This Week in Rails

September 9, 2023

Infer primary_key: :id on associations with composite primary key models, add validation option for enum and more!

Hi! This is Vipul bringing you the latest from this week’s changes in the Rails codebase.

Prior to this change, you’d need to do the following to set up associations for composite primary key models:

class Order
  self.primary_key = [:shop_id, :id]
  has_many :order_agreements, primary_key: :id
end

class OrderAgreement
  belongs_to :order, primary_key: :id
end


After this change, the primary_key option no longer needs to be specified:

class Order
  self.primary_key = [:shop_id, :id]
  has_many :order_agreements
end

class OrderAgreement
  belongs_to :order
end

This change adds :validate option for enums. If you want the enum value to be validated before saving, use the option :validate :

class Conversation < ApplicationRecord
  enum :status, %i[active archived], validate: true
end

conversation = Conversation.new
conversation.status = :unknown
conversation.valid? # => false


It is also possible to pass additional validation options:

class Conversation < ApplicationRecord
  enum :status, %i[active archived], validate: { allow_nil: true }
end

conversation = Conversation.new
conversation.status = nil
conversation.valid? # => true


Otherwise ArgumentError will raise which is standard current behavior:

class Conversation < ApplicationRecord
  enum :status, %i[active archived]
end

conversation = Conversation.new
conversation.status = :unknown # 'unknown' is not a valid status (ArgumentError)

Ruby 3.3 added a change that allows passing a list of characters to SecureRandom.alphanumeric. This change now uses SecureRandom.alphanumeric for SecureRandom.base36/base58 instead of complex SecureRandom.random_number/random_bytes usage as well for a slightly faster solution.
Specifying Relation#merge(rewhere: true) is deprecated, as that has now been the default since Rails 7.0. This change will warn about setting the rewhere option will error in Rails 7.2.
This change fixes unscope not working in specific case for triple dot range. For example:

Before:
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts` WHERE `posts`.`id` >= 1 AND `posts`.`id` < 3"    


After:
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts`"

This change fixes change_column not setting precision: 6 on datetime columns when using 7.0+ migrations and SQLite.
With the changes made in previously, has_secure_token declarations can be configured to execute in an after_initialize callback. This commit adds a new Rails 7.1 default: generate all has_secure_token values when their corresponding models are initialized.
By default simple_format method returns the text wrapped with <p>. But if we explicitly specify the wrapper_tag: nil in the options, it used to return the text wrapped with <></> tag. This change fixes the behaviour to now wrap it in <p> instead.

You can view the whole list of changes here.
We had 30 contributors to the Rails codebase this past week!

Until next time!

About This Week in Rails

Your weekly inside scoop of interesting commits, pull requests and more from Rails.