In my model I have:
attr_accessor :ppris
def last(x)
@ppris = []
if @ppris == nil
@ppris << 1
else
@ppris << 2
end
end
def pakke
if self.pakkes.count > 0
self.pakkes.each{|pakke| beregn_pakke(pakke.price)}
end
end
In view a have a normal loop for my Company model.
<%= company.ppris %>
In view the value for ppris is [2] for all rows.
I have 5 Company rows in my table. I would expect that the value for ppris would be:
[1, 2, 2, 2, 2] for each company.
I have also changed the model method to:
def last(x)
@ppris = []
if @ppris == nil
@ppris << 1
else
@ppris << @ppris.last + 1
end
end
But then I get a NoMethodError. I would expect no error an ppris be:
[1, 2, 3, 4, 5]
What is wrong with my model method? Why can’t I create an array that gets added values for each pakke?
@ppriswill never benil. It’ll beempty?, but notnil.Addressing your comment
You create a new array every time you call the method. If you want a single array associated with an instance, create one in the
after_initializecallback.