I get error:
index -7 out of array
What is wrong with my array? I can’t store them too many digits, or what?
PS – (From json I get only ids)
answers_from_json = ActiveSupport::JSON.decode(params[:answers_from_json]) answers_from_json.each_with_index do |item, i| posts = Post.find(:all, :conditions => ["id=?",item.to_i]) sheet[header_y_offset-1,0] = 'Name' posts.each_with_index do |post,i| sheet[1+i,0] = post.name end end
You are reading a value from an array, thats not there – expecting an array to have more elements in it than it actually does. Take another look at your
sheet array, it may not have the
i+1 items in it.
EDIT : Keep in mind that with 0-based index, the last index of an array of length 7 is 6 😉
Just print out it’s length and the index you are trying to access.Then you might see the issue:
posts.each_with_index do |post,i| p "length of the post: " + post.size p "trying to access element nr. : " + (i + 1) sheet[1+i,0] = post.name end
If the error is not raised here, then it might be the
sheet[header_y_offset-1,0]statement or a similar statement, where the error actually happens – you get the picture I hope. The optimal way would be to debug – but as I don’t know ruby beyond thr basics – I can only advise on how to print-debug – it should be enough.