从设计的角度来看,实例化 ActiveModel::Model 类本身提供了什么?

What does instantiating an ActiveModel::Model class within itself provide from a design perspective?

提问人:J.R. Bob Dobbs 提问时间:10/18/2023 更新时间:10/18/2023 访问量:6

问:

假设 PORO 如下:

class Player
  include ActiveModel::Model
  include ActiveModel::Validations

  attr_accessor :name, :position
  validates :name, :position
  

  VALID_POSITIONS = ['goalie', 'defense', 'forward']
  validates :position, :inclusion { in: VALID_POSITIONS}

  BENCH_WARMER = Player.new(name: "Biz", position: "bench_warmer")
end
  1. 如何取代为属性明确声明的验证?BENCH_WARMERposition

  2. 这个实例化本身为应用程序提供了什么/既然它存在于这个类中,我能用它做什么?BENCH_WARMERBENCH_WARMER

  3. 为什么要验证它是否只是在下一行中被忽略?

在谷歌上搜索这类问题指向了一堆关于使用和如何在其外部创建对象实例的基本资源。self

验证 实例化 Ruby-on-Rails-7

评论


答: 暂无答案