Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined method 'each_with_index' after upgrading Rails from 4.2.8 to 5.0.3 #83

Closed
holgum opened this issue May 31, 2017 · 19 comments
Closed

Comments

@holgum
Copy link

holgum commented May 31, 2017

I have a simple template that was working fine before upgrading to Rails 5.0.3:

wb = xlsx_package.workbook
wb.add_worksheet(name: 'Servers') do |sheet|
	sheet.add_row ['Hostname', 'CPU Count']
end

After upgrading to Rails 5.0.3 I get:

undefined method `each_with_index' for #<Axlsx::Row:0x5e4b6ae7>
Did you mean?  each_index
Extracted source (around line #4): 
            wb.add_worksheet(name: 'Servers') do |sheet|
	sheet.add_row ['Hostname', 'CPU Count']

The problem (which I can duplicate from the Rails console) is occurring as soon as 'add_row' is called.
My Gemfile (before and after Rails upgrade):

gem 'rubyzip', '>= 1.2.1'
gem 'axlsx', git: 'https://github.com/randym/axlsx.git', ref: '776037c0fc799bb09da8c9ea47980bd3bf296874'
gem 'axlsx_rails'

Actual version of asxlx_rails in Gemfile.lock is '0.5.1' in both cases.

Any idea if this is a bug or I am missing something?

@straydogstudio
Copy link
Collaborator

Hm. Something is up with Axlsx and Rails. Can you post a bigger stack trace?

@straydogstudio
Copy link
Collaborator

@holgum Did you try Axlsx 2.1.0.pre?

There is activity on the issue of the next release of Axlsx (3.00). That should solve many issues.
randym/axlsx#501

@holgum
Copy link
Author

holgum commented May 31, 2017

Here is stacktrace from console (forgot to mention this is JRuby - same was used w/ Rails 4.2)

irb(main):006:0> s.add_row["Hello"]
NoMethodError: undefined method `each_with_index' for #<Axlsx::Row:0x72e36677>
Did you mean?  each_index
        from C:/bin/jruby-9.1.10.0/lib/ruby/gems/shared/bundler/gems/axlsx-776037c0fc79/lib/axlsx/workbook/worksheet/worksheet.rb:
781:in `update_column_info'
        from C:/bin/jruby-9.1.10.0/lib/ruby/gems/shared/bundler/gems/axlsx-776037c0fc79/lib/axlsx/workbook/worksheet/worksheet.rb:
446:in `add_row'
        from (irb):6:in `<eval>'
        from org/jruby/RubyKernel.java:1000:in `eval'
        from org/jruby/RubyKernel.java:1298:in `loop'
        from org/jruby/RubyKernel.java:1120:in `catch'
        from org/jruby/RubyKernel.java:1120:in `catch'
        from C:/bin/jruby-9.1.10.0/lib/ruby/gems/shared/gems/railties-5.0.3/lib/rails/commands/console.rb:65:in `start'
        from C:/bin/jruby-9.1.10.0/lib/ruby/gems/shared/gems/railties-5.0.3/lib/rails/commands/console_helper.rb:9:in `start'
        from C:/bin/jruby-9.1.10.0/lib/ruby/gems/shared/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:78:in `console'
        from C:/bin/jruby-9.1.10.0/lib/ruby/gems/shared/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:49:in `run_comman
d!'
        from C:/bin/jruby-9.1.10.0/lib/ruby/gems/shared/gems/railties-5.0.3/lib/rails/commands.rb:18:in `<main>'
        from org/jruby/RubyKernel.java:961:in `require'
        from bin/rails:4:in `<main>'
irb(main):007:0>```

@holgum
Copy link
Author

holgum commented May 31, 2017

@straydogstudio - can't use 2.1.0-pre as I need rubyzip > 1.2.1

@holgum
Copy link
Author

holgum commented May 31, 2017

Tried
gem 'axlsx', git: 'https://github.com/randym/axlsx.git', branch: 'release-3.0.0'

Same error.

@holgum
Copy link
Author

holgum commented Jun 1, 2017

Swapped out jruby-9.1.10.0 for ruby-2.3.4 and it works fine, so must be an issue w/ JRuby.

@straydogstudio
Copy link
Collaborator

I was wondering about that. Thanks for the info. Hopefully you aren't too dependent on jRuby.

Row comes from SimpleTypedList, which is an Axlsx class which delegates to array methods. each_with_index should be delegated, since it isn't actually there. Something isn't working right with that and jRuby's array class I expect.

Might be worth watching, although it says the issue is resolved for 9.1.10.0: jruby/jruby#4610

@holgum
Copy link
Author

holgum commented Jun 1, 2017

I don't think I would call this 'closed' just yet.
It worked with Rails 4.2 and the same version of JRuby, so it would seem it's not strictly JRuby but rather JRuby + Rails 5 that is the issue. I don't understand the dependency between axlsx_rails and actionpack, but maybe something changed there between Rails 4.2 and 5.0 that broke it when using JRuby?

Unfortunately we are stuck with JRuby on this project - which means we are stuck at Rails 4.2 until this is resolved. Or use another solution for exporting to xls.

Tried with JRuby 9.1.3.0 which I already had installed. Same problem.

@straydogstudio
Copy link
Collaborator

Actionpack is where the abstract controller is, which is what axlsx_rails interacts with, being a renderer. It is a lighter weight dependency than all of rails. That's a pretty general dependency. I wouldn't expect it to be the issue, but you never know. I've reopened the issue.

Have you tried generating the report entirely in the controller, without axlsx_rails as a dependency, just axlsx? I would not expect any difference, but it will make it clear that the problem is axlsx, not axlsx_rails.

xlsx_package = Axlsx::Package.new()
#your template code
send_data xlsx_package.to_stream.read, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename: "filename.xlsx"

@straydogstudio
Copy link
Collaborator

Or it will make it clear that there is a dependency problem.

@holgum
Copy link
Author

holgum commented Jun 1, 2017

Tried putting code above into controller - same error.

undefined method `each_with_index' for #<Axlsx::Row:0x44aa1bd5> Did you mean? each_index

so I guess axlsx_rails is off the hook?

@straydogstudio
Copy link
Collaborator

Looks like it. If I have a chance I'll poke around in it anyway. I suggest you create an issue on the Axlsx gem. I searched and didn't see any related issue. But reference this issue if you do.

@straydogstudio
Copy link
Collaborator

I've confirmed using an isolated environment (a Docker jRuby image) that each_with_index doesn't come along with Array.instance_methods here: https://github.com/randym/axlsx/blob/d136835f3a3a54831ad2c7c793a51d3a57273892/lib/axlsx/util/simple_typed_list.rb#L158

@straydogstudio
Copy link
Collaborator

I have a hack for a given Rails app. In config/application.rb, right before the Bundler.require* statement, add this line:

Array.include Enumerable

This seems rather unnecessary. But it does work.

@straydogstudio
Copy link
Collaborator

straydogstudio commented Jun 2, 2017

To show each_with_index is missing in Rails with Docker:

docker run -it --rm -p 3000:3000 --name arraytest jruby /bin/bash
# gem install rails -v 5.0.3
# gem install listen
# rails new arraytest -O
# cd arraytest
# echo "gem 'listen', group: :development" >> Gemfile
# bundle exec rails c
irb > Array.instance_methods.select {|m| m =~ /each/}

@holgum
Copy link
Author

holgum commented Jun 2, 2017

And yet running it outside of the Rails console shows each_with_index is there in JRuby 9.1.10.0.
Where should this be reported then?

I'm good with your workaround for now, though. Thanks.

@straydogstudio
Copy link
Collaborator

I think it will end up in Rails. I'm doing what I can to test MRI with the same setup, and see the difference in Array.instance_methods. That should be reported on a Rails issue.

@straydogstudio
Copy link
Collaborator

Confirmed each_with_index is also missing in Rails 5.1, and present with MRI. Rails issue: rails/rails#29329

@straydogstudio
Copy link
Collaborator

Submitted to JRuby where it is a duplicate of #4723. Should be fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants