Topic: NoMethodError

I am getting a strange error like



NoMethodError in Am basController#update

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.+

RAILS_ROOT: C:/InstantRail/rails_apps/portfolio/config/..
Application Trace | Framework Trace | Full Trace

C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/locking.rb:37:in `update_without_callbacks'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:278:in `update_without_timestamps'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/timestamp.rb:39:in `update'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1718:in `create_or_update_without_callbacks'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:253:in `create_or_update'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1392:in `save_without_validation'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:736:in `save_without_transactions'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in `save'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in `transaction'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:91:in `transaction'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:118:in `transaction'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in `save'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1439:in `update_attributes'
#{RAILS_ROOT}/app/controllers/am_bas_controller.rb:38:in `update'
-e:3:in `load'
-e:3

in spite of the fact that I have that method defined in the controller class

Here is my controller class

class AmBasController < ApplicationController
  layout nil
  def index
    list
    render :action => 'list'
  end



  def list
    @am_ba_pages, @am_bas = paginate :am_bas, :per_page => 10
  end

  def show
    @am_ba = AmBa.find(params[:id])
  end

  def new
    @am_ba = AmBa.new
  end

  def create
    @am_ba = AmBa.new(params[:am_ba])
    if @am_ba.save
      flash[:notice] = 'AmBa was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @am_ba = AmBa.find(params[:id])
  end

  def update
    @am_ba = AmBa.find(params[:id])
    if @am_ba.update_attributes(params[:am_ba])
      flash[:notice] = 'AmBa was successfully updated.'
      redirect_to :action => 'show', :id => @am_ba
    else
      render :action => 'edit'
    end
  end

  def destroy
    AmBa.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end


Any idea why that is hapening?

Thanks

Re: NoMethodError

It is finding the "update" method in the controller okay. The problem is occurring deep in the Active Record code during the update. This makes me think there is something wrong with how the model is set up or the values which are attempting to be updated. If you can't find the problem, can you post your model's database schema, am_ba.rb file, and the params this update action is receiving from the form (it should be on that error page).

Last edited by ryanb (2006-11-14 20:21:15)

Railscasts - Free Ruby on Rails Screencasts

Re: NoMethodError

That is migration with which I created the am_bas table

class CreateAmBas < ActiveRecord::Migration
  def self.up
    create_table :am_bas do |t|
       t.column :business_area, :string
       t.column :business_owner, :string
       t.column :primary_crg, :string
       t.column :am_ba_department_id, :string
       t.column :remarks, :string
       t.column :created_on, :datetime 
       t.column :created_at, :datetime 
       t.column :updated_on, :datetime 
       t.column :updated_at, :datetime 
       t.column :lock_version, :integer
    end
    create_table("am_applications_am_bas", :id=>false) do |t|
      t.column "am_application_id", :integer
      t.column "am_ba_id", :integer
    end
  end

  def self.down
    drop_table :am_bas
  end
end


The request which comes on the error page is

Request

Parameters: {"am_ba"=>{"business_area"=>"Accounts payable", "remarks"=>"", "business_owner"=>"Kevin", "primary_crg"=>""}, "commit"=>"Edit", "id"=>"10001"}

and finally in Orcale this is how the table structure is created

Column Name    Data Type    Nullable    Default    Primary Key
ID    NUMBER(38,0)    No    -     1
BUSINESS_AREA    VARCHAR2(255)    Yes    -     -
BUSINESS_OWNER    VARCHAR2(255)    Yes    -     -
PRIMARY_CRG    VARCHAR2(255)    Yes    -     -
AM_BA_DEPARTMENT_ID    VARCHAR2(255)    Yes    -     -
REMARKS    VARCHAR2(255)    Yes    -     -
CREATED_ON    DATE    Yes    -     -
CREATED_AT    DATE    Yes    -     -
UPDATED_ON    DATE    Yes    -     -
UPDATED_AT    DATE    Yes    -     -
LOCK_VERSION    NUMBER(38,0)    Yes    -     -

Re: NoMethodError

Hi Ryanb,
I think I know why i am getting the error. It is because of the lock_version column.
I changed that name and now it is behaving again.

Thanks for you time and inputs.