升级到state_machines gem时的验证问题

时间:2015-11-03 21:03:53

标签: ruby-on-rails ruby-on-rails-4 state-machine

我想从state_machine更改为state_machines。当我开始这样做时,我遇到了状态不再从初始状态改变。

回顾了一些stackoverflow和其他指南: -

我所做的改变:

  1. 使用state-machines-activerecord gem

  2. 添加def初始化... (见下文)

  3. 我现在收到数据上的rake设置错误。它 似乎有一个验证联系人的问题(一个相关的 首先创建的模型)。无论状态如何,联系人都是 总是先创建。这在改变宝石之前运行良好。
  4. 任何想法?我想我已经包含了所有相关的代码。

    宝石文件:

    ruby "2.2.2"
    gem "rails", "4.2.1"
    gem "pg", "0.17.1" # postgresql database
    gem "state_machines-activerecord"
    

    gem-lock文件

    state_machines (0.4.0)
    state_machines-activemodel (0.3.0)
      activemodel (~> 4.1)
      state_machines (>= 0.4.0)
    state_machines-activerecord (0.3.0)
      activerecord (~> 4.1)
      state_machines-activemodel (>= 0.3.0)`
    

    lead.rb(型号)是:

    `class Lead < ActiveRecord::Base
      belongs_to :created_by_user, class_name: "User", inverse_of: :leads_created
      belongs_to :contact
      belongs_to :user
      accepts_nested_attributes_for :contact
      validates :contact, presence: true
    state_machine :state, initial: :new_lead do
        state :claimed
        state :referred
        state :broadcast
        state :unclaimed`
    
        `event :claim! do
          transition all => :claimed
        end
    
        event :unclaim! do
          transition claimed: :unclaimed
        end
    
        event :refer! do
          transition new_lead: :referred
        end
    
        event :broadcast! do
          transition all => :broadcast
        end`
    
    
    
    `def initialize(*)
        super() # NOTE: This *must* be called, otherwise states won't get   
        initialized
      end`
    

    CONTROLLER

    def lead_attributes
        params.require(:lead).permit(   :claimed, 
          :contact_id,
          :status,
          :state,
          :user_id
    

    setup_acceptance.rake

    def create_contact(options={})
      user               = User.find_by(last_name: "Smith")
      contact_attributes = { created_by_user: user, user: user }
      attributes         = contact_attributes.merge options
      contact = Contact.create! attributes
      contact.save!
      contact
    end
    def create_lead(options={})
      user              = User.find_by(last_name: "Smith")
      client_attributes = { user: user, created_by_user: user }
      attributes        = client_attributes.merge options
      Lead.create! attributes  ****(LINE 1311 where error message occurs)****
    end
    

    rake.setup

    Lead.transaction do #(NOTE: THIS IS LINE 435)
        create_lead(  #(NOTE: THIS IS LINE 436)
          user: User.find_by(last_name: "Smith"),
          contact: Contact.find_by(last_name: "Lozar"),
          status: 0,
          state: "claimed"
            }
          ]
        )
    

    错误:

    rake aborted!
    ActiveRecord::RecordInvalid: Validation failed: Contact Please enter a value
    /Users/workspace/ab/lib/tasks/setup_acceptance.rake:1311:in `create_lead'
    /Users/workspace/ab/lib/tasks/setup.rake:436:in `block (2 levels) in <top (required)>
    /Users/workspace/ab/lib/tasks/setup.rake:435:in `block in <top (required)>'
    Tasks: TOP => setup_sample_data
    (See full trace by running task with --trace)
    

1 个答案:

答案 0 :(得分:1)

非常感谢@avdi

解决方案是改变:

super()

...丢弃所有初始化参数

为:

super

......隐含地保留了它们。