1

I have two arrays which I would like to initialise to [] in my Prediction model's object.

If I try:

def initialize
    @first_estimation = []
    @last_estimation = []
end

Then many of my unit tests fail.

 Failure/Error: assign(:prediction, Prediction.new(
     ArgumentError:
       wrong number of arguments (1 for 0)

However, if I try:

def after_initialize
    @first_estimation = []
    @last_estimation = []
end

Then the arrays do not get instantiated.

How can I instantiate the arrays when the object is constructed without altering anything else?

1 Answer 1

5

Since I see a parenthesis here Prediction.new( I deduce you try to pass params.

But your initialize doesnt accept any => boom


But wait, is it an ActiveRecord model?

If so, you have to use:

 after_initialize :set_arrays

 def set_arrays
   @first_estimation = []
   @last_estimation = []
 end

and maybe use attr_accessor too, but dont know what you expect

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.