简体   繁体   中英

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. 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.

When I create a new User and save it (using the callback after_create of the user)

 t = Talent.create(user: self)

The byebug its not called.

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?

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 is evaluated when the class is loaded, that's why byebug only stops the excecution on those moments. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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