I have a model called “User” with has many association to “Address” model.
When I do User.new.addresses.build, I am getting following error,
ArgumentError: wrong number of arguments (1 for 0)
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:162:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:162:in `new'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:162:in `build_association'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:423:in `build_record'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:102:in `build'
from (irb):10
This happens when i add an initialize method in Address model.
Any help is highly appreciated…
EDITED:
There are no arguments to the initialize method and it looks like this
class Address < ActiveRecord::Base
attr_accessor :test
def initialize
test = "manu"
end
end
It’s not recommended to override the initialize method of an ActiveRecord inherited class.
Use
after_initializecallback if you want to add your custom initialization. See the rails api for all the ActiveRecord callbacks.