大家好!我是 Vipul,为您带来本周 Rails 代码库最新更改的汇总。
推断关联上的 primary_key: :id,用于复合主键模型
在此更改之前,您需要执行以下操作来为复合主键模型设置关联
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
在此更改之后,不再需要指定 primary_key 选项
class Order
self.primary_key = [:shop_id, :id]
has_many :order_agreements
end
class OrderAgreement
belongs_to :order
end
为 enum 添加验证选项,使其能够在不引发错误的情况下进行验证
此更改为 enum 添加了 :validate 选项。如果您希望在保存之前验证 enum 值,请使用 :validate 选项
class Conversation < ApplicationRecord
enum :status, %i[active archived], validate: true
end
conversation = Conversation.new
conversation.status = :unknown
conversation.valid? # => false
也可以传递额外的验证选项
class Conversation < ApplicationRecord
enum :status, %i[active archived], validate: { allow_nil: true }
end
conversation = Conversation.new
conversation.status = nil
conversation.valid? # => true
否则将引发 ArgumentError,这是标准的当前行为
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 中使用 SecureRandom.alphanumeric 替代 SecureRandom.base36/base58
Ruby 3.3 添加了一项更改,允许将 字符列表传递给 SecureRandom.alphanumeric。此更改现在使用 SecureRandom.alphanumeric 来替代 SecureRandom.base36/base58,而不是复杂的 SecureRandom.random_number/random_bytes 使用,并且解决方案也稍快一些。
指定 Relation#merge(rewhere: true) 已弃用,因为自 Rails 7.0 起这已成为默认行为。此更改将发出警告,指示在 Rails 7.2 中设置 rewhere 选项将导致错误。
修复 unscope 在 where 使用三点范围时不起作用的问题
此更改修复了 unscope 在特定情况下对三点范围不起作用的问题。例如
之前
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts` WHERE `posts`.`id` >= 1 AND `posts`.`id` < 3"
之后
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts`"
修复 change_column 未为 sqlite 设置 precision 的问题
此更改修复了在使用 7.0+ Migrations 和 SQLite 时,change_column 未在 datetime 列上设置 precision: 6 的问题。
将 has_secure_token 的默认值更改为 on: :initialize
通过 之前的更改,has_secure_token 声明 可以配置为在 after_initialize 回调中执行。此提交为 Rails 7.1 添加了一个新默认值:在初始化模型时生成所有 has_secure_token 值。
修复:simple_format 带有空白 wrapper_tag 选项时返回纯 HTML 标签
默认情况下,simple_format 方法返回用 <p> 包裹的文本。但是,如果我们显式地在选项中指定 wrapper_tag: nil,它过去会返回用 <></> 标签包裹的文本。此更改修复了该行为,现在将其包裹在 <p> 中。
您可以在此处查看所有更改的完整列表 。 上周,我们有 30 位贡献者 为 Rails 代码库做出了贡献!
下次再见!
订阅以通过邮件获取这些更新。