user2970050
user2970050

Reputation: 307

Validation issue when upgrading to state_machines gem

I wanted to change from state_machine to state_machines. As I started to do this I encountered that the state was no longer changing from the initial state.

reviewed some stackoverflow and other guides: -

So changes i have made:

  1. using state-machines-activerecord gem

  2. add def initialize... (see below)

  3. I am now getting a rake setup error on my data. it seems to have an issue with the validation the Contact ( a related model that gets created first). Regardless of state, the Contact is always created first. This ran fine before changing the gem.

any ideas? i think i have included all relevant code.

gem-file:

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

gem-lock file

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 (model) is:

`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"
        }
      ]
    )

error:

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)

Upvotes: 1

Views: 349

Answers (1)

user2970050
user2970050

Reputation: 307

with great thanks @avdi

The solution was to change:

super()

...which discards all the initialization arguments

to:

super

...which retains them implicitly.

Upvotes: 1

Related Questions