升级到 state_machines gem 时的验证问题
Validation issue when upgrading to state_machines gem
我想从 state_machine 更改为 state_machine。当我开始这样做时,我遇到状态不再从初始状态改变。
查看了一些 Whosebug 和其他指南:-
- https://teamtreehouse.com/community/statemachines-usage
- state-machines/state_machines-activerecord#33
- Validation before persistance on state_machine gem
所以我做了以下更改:
使用状态机-activerecord gem
添加 def 初始化...
(见下文)
- 我现在在我的数据上遇到了 rake 设置错误。它
联系人的验证似乎有问题(相关的
首先创建的模型)。无论状态如何,联系人都是
总是先创建。这个运行在改gem之前还不错。
有什么想法吗?我想我已经包含了所有相关代码。
gem-文件:
ruby "2.2.2"
gem "rails", "4.2.1"
gem "pg", "0.17.1" # postgresql database
gem "state_machines-activerecord"
gem-锁定文件
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)
非常感谢@avdi
解决方案是更改:
super()
...丢弃所有初始化参数
至:
super
...隐式保留它们。
我想从 state_machine 更改为 state_machine。当我开始这样做时,我遇到状态不再从初始状态改变。
查看了一些 Whosebug 和其他指南:-
- https://teamtreehouse.com/community/statemachines-usage
- state-machines/state_machines-activerecord#33
- Validation before persistance on state_machine gem
所以我做了以下更改:
使用状态机-activerecord gem
添加 def 初始化... (见下文)
- 我现在在我的数据上遇到了 rake 设置错误。它 联系人的验证似乎有问题(相关的 首先创建的模型)。无论状态如何,联系人都是 总是先创建。这个运行在改gem之前还不错。
有什么想法吗?我想我已经包含了所有相关代码。
gem-文件:
ruby "2.2.2"
gem "rails", "4.2.1"
gem "pg", "0.17.1" # postgresql database
gem "state_machines-activerecord"
gem-锁定文件
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)
非常感谢@avdi
解决方案是更改:
super()
...丢弃所有初始化参数
至:
super
...隐式保留它们。