【问题标题】:Validation issue when upgrading to state_machines gem升级到 state_machines gem 时的验证问题
【发布时间】:2015-11-03 21:03:52
【问题描述】:

我想从 state_machine 更改为 state_machines。当我开始这样做时,我发现状态不再从初始状态发生变化。

查看了一些 stackoverflow 和其他指南:-

所以我做了改变:

  1. 使用 state-machines-activerecord gem

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

  3. 我的数据现在出现 rake 设置错误。它 似乎对联系人的验证有问题(相关的 首先创建的模型)。无论状态如何,联系人都是 总是先创建。这在更改 gem 之前运行良好。

有什么想法吗?我想我已经包含了所有相关的代码。

宝石文件:

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

宝石锁文件

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`

控制器

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)

【问题讨论】:

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


    【解决方案1】:

    非常感谢@avdi

    解决办法是改变:

    super()
    

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

    到:

    super
    

    ...隐式保留它们。

    【讨论】:

      猜你喜欢
      • 2011-06-01
      • 1970-01-01
      • 2011-02-04
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 2019-04-03
      相关资源
      最近更新 更多