简体   繁体   English

Rails中with_options的生命周期。 为什么不工作?

[英]Lifecycle of with_options in Rails. Why not working?

Im with a very tricky problem with a validation code on my app. 我的应用程式上的验证码有一个非常棘手的问题。

I have a user and a talent. 我有用户和才能。 A User has_one Talent. 用户has_one天赋。 And, depending on the steps of the application, I need to validate some cases. 并且,根据应用程序的步骤,我需要验证一些情况。

Those are my classes. 那是我的课。

class User < ApplicationRecord
  has_one :talent, dependent: :destroy
  has_one :client, dependent: :destroy
  has_one :agency, dependent: :destroy
  before_create :set_initial_step
  after_create :build_type

  def build_type
    logger.debug("Im inside build_type ***************** with #{type}")

    if type.nil?
      t = Talent.create(user: self)
      Rails.logger.debug("checking errors ******#{type}*********** with #{t.errors.full_messages}")
    else
      o = Object.const_get(type.capitalize).create(user: self)
      Rails.logger.debug("Im inside build_type ****#{type}************* with #{o.errors.full_messages}")
    end
  end

class Talent < ApplicationRecord

with_options if: Proc.new { |t| t.user.approval_submission_step >= 2 } do |t|
byebug
t.validates :talent_type_id,
  :rate,
  presence: true
t.validates :rate, numericality: { greater_than_or_equal_to: 25 }
 end

  with_options if: Proc.new { |t| t.user.approval_submission_step >= 3 && t.talent_type.is_model } do |t|

t.validates :gender,
  :ethnicity,
  :height_feet,
  :height_inches,
  :weight,
  :eye_color,
  :hair_color,
  presence: true
t.validates :men_pant_size_waist,
  :men_pant_size_length,
  :men_shirt,
  :men_dress_shirt_size,
  :men_dress_shirt_neck,
  :men_dress_shirt_sleeve,
  :men_jacket,
  :men_shoe_size,
  presence: true, if: Proc.new { |t| t.gender == 'male' }
t.validates :ethnicity,
            :inclusion => {in: VALID_ETHNICITY_TYPES}
t.validates :womens_dress_size,
  :women_shoe_size,
  :women_shirt,
  :womens_dress_size,
  :women_pant,
  :women_bra_cup,
  :women_bra_size,
  presence: true, if: Proc.new { |t| t.gender == 'female' }
 end

First weird thing. 第一件事。 The byebug in the middle of the with_options its called when the server starts or when I do a rails console. with_options中间的byebug在服务器启动或执行Rails控制台时被调用。

When I create a new User and save it (using the callback after_create of the user) 当我创建一个新用户并保存时(使用用户的after_create回调)

 t = Talent.create(user: self)

The byebug its not called. 该byebug未被调用。

What Im doing wrong? 我在做什么错? Why the with_options on Talents are being called on the class-loading process but not when creating a new one? 为什么在类加载过程中调用Talents的with_options而不是在创建新类时调用它?

First weird thing. 第一件事。 The byebug in the middle of the with_options its called when the server starts or when I do a rails console. with_options中间的byebug在服务器启动或执行Rails控制台时被调用。

with_options is evaluated when the class is loaded, that's why byebug only stops the excecution on those moments. with_options是在加载类时评估的,因此,byebug仅在这些时刻停止执行。 That's how it works. 就是这样。

I'm not really sure what you want to do with those byebug calls, but I think you want to add them inside the Proc's, that code is actually evaluated at the moment with the current instance. 我不太确定要如何处理这些byebug调用,但是我想将它们添加到Proc中,该代码实际上是在当前实例的情况下进行评估的。

with_options if: Proc.new { |t| byebug; t.user.approval_submission_step >= 2 } do |t|

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM