Joe Half Face
Joe Half Face

Reputation: 2323

Wrong number of arguments( 0 for 2..3)

I want to refactor a string, which I receive as a parameter, into an array of words (using the split method).

My Test model has one attribute, called source.

class Test < ActiveRecord::Base
  attr_accessible :source
  serialize :sources, Array
  after_create do
    test.source.split(' ')
  end
end

This returns an error:

wrong number of arguments (0 for 2..3)

I can't figure out what arguments Rails wants.

UPD

And if I change my code like this:

class Test < ActiveRecord::Base
  attr_accessible :source
  serialize :sources, Array
  def split_this_text(test_id)
    @test = Test.where(:test_id=>test_id)
    @test.source.split(' ')
  end
end

and call method in tests_controller/create:

 @test.split_this_text(:id)

Than I get this error:

NoMethodError in TestsController#create

undefined method `split' for #<Arel::Nodes::JoinSource:0x4ddfc60>

UPD#2

Finally works works without any mistakes, but behaves like nothing works and source usuall string (for example @test.source[0] returns a letter)

class Test < ActiveRecord::Base
  attr_accessible :source
  serialize :sources, Array
  before_save :split_this_text
  def split_this_text
    self.source.split(' ')
 end
end

Upvotes: 2

Views: 2386

Answers (1)

s2t2
s2t2

Reputation: 2686

There is a possibility that the word "test" is reserved by rails in some way.

One of my model's attribute names was "test". When I tried to call instance.update_attributes(:test => SOMEVALUE), it would return the same 'wrong number of arguments (0 for 2..3)' message you were getting.

When I changed the name of the attribute from "test" to something else, the error disappeared.

Upvotes: 1

Related Questions