brendon / acts_as_list

An ActiveRecord plugin for managing lists.
http://brendon.github.io/acts_as_list/
MIT License
2.05k stars 356 forks source link

NoMethodError: undefined method `acts_as_list' #303

Closed oyeanuj closed 6 years ago

oyeanuj commented 6 years ago

Hey @brendon, I ran into an issue which feels awkwardly similar to #176 but the solution mentioned there doesn't seem to work for me.

I am on Rails 5.1.4 (API-only mode), and did the following as per the README:

  1. Added it to Gemfile and installed the gem.
  2. Created a column for the position in the intended model.
  3. Added the following code snippet.

    belongs_to        :member
    validates       :member,
    presence:       true
    
    acts_as_list      scope:  :member
  4. And got the following error:
    [100] pry(main)> Movie
    NoMethodError: undefined method `acts_as_list' for #<Class:0x00007f9b373ca6c0>
    Did you mean?  acts_like?
    from /Users/anuj/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/activerecord-5.1.4/lib/active_record/dynamic_matchers.rb:22:in `method_missing'

I assume I am missing some obvious step, but can't seem to figure out what that might be. I went through #176, and the solution mentioned there to include include ActiveRecord::Acts::List throws a different kind of error for me.

Any thoughts on what might be going on are super appreciated?

brendon commented 6 years ago

Hi @oyeanuj, it definitely looks like acts_as_list isn't being required for some reason (or at least the acts_as_list method isn't being added to ActiveRecord::Base.

The class methods are added via this:

https://github.com/swanandp/acts_as_list/blob/master/lib/acts_as_list/active_record/acts/active_record.rb

So if active_record isn't loading then perhaps that's the problem. Since you're restricting what parts of Rails are loaded, can you double check that you're indeed loading ActiveRecord? and doing it the right way? :)

oyeanuj commented 6 years ago

@brendon Thanks for the quick response! That makes sense but I am not sure why that might be happening since I've tested it with a clean Rails-API mode which includes ActiveRecord (I use it everywhere!)

Here is my config/application.rb if that helps: https://gist.github.com/oyeanuj/6b6db7f6b239c00f6039883bb3969919

brendon commented 6 years ago

Thanks @oyeanuj :) It doesn't look like you're loading in active_support though I can't be sure. Perhaps that's always loaded anyway? If it's not, that might be the reason why the on_load callback isn't being called and thus aal isn't loading. Can you do some investigation down that track? Perhaps you only need to include a small part of active support to do with loading?

oyeanuj commented 6 years ago

So, it seems like ActiveSupport.onLoad is being called. In one of the initializer files, I added the following code which got printed:

ActiveSupport.on_load(:active_record) do
 ap "Please work"
end

In general, FWIW, here is the list of middleware that an API-app comes with, by default: http://edgeguides.rubyonrails.org/api_app.html#choosing-middleware

Any ideas on what might be going on, given all of this?

Appreciate you taking the time to figure this out!

brendon commented 6 years ago

No worries :)

In that case what I'd do next is clone a local copy of acts_as_list and update your gem file to refer to that. Then you can start playing around with how the gem is loaded to see if you can figure out where it's going wrong. Try adding a logging line in our on_load callback to see if that logs anything etc...

It could be that we're initialising incorrectly somehow but hard to say :D

bevinh commented 6 years ago

Not working for me either. Did you find a fix @oyeanuj?

brendon commented 6 years ago

Hi @bevinh, can you try the suggestion in my last comment? You need to pinpoint the loading sequence problem so we can figure out a way around it.

oyeanuj commented 6 years ago

@brendon @bevinh I haven't had a chance to go back to this but I am hoping to do this soon.

@bevinh If you are able to test out the suggestion above sooner, that would be great!

bevinh commented 6 years ago

I've just noticed that it works if it's scoped (e.g. acts_as_list scope: :list) but does not if it isn't. I was using an example from GoRails where the main list was just used with acts_as_list (no scope). I'm not sure what the intention of not scoping it is in that tutorial. I haven't had a chance to dig much deeper, as I'm working on a different app right now, but hopefully that is helpful. Did something change that requires the scope?

brendon commented 6 years ago

Lol, I've never known it not to require a scope :) I suppose it doesn't need one though. I'm not even sure there's tests for that... Might be something to investigate if you have the time :)

oyeanuj commented 6 years ago

@brendon Sorry for the delay here. So, to your question about ActiveSupport, it is being loaded:

[8] pry(main)> defined?(ActiveSupport.on_load)
=> "method"

It doesn't seem like API-only apps do anything to ActiveSupport

Thoughts on what else might be going on? Anything related to @bevinh's comment above?

brendon commented 6 years ago

The only idea I have is to build a new empty (full) rails app. Then confirm it works with acts_as_list. Next start stripping off the bits you don't need and check if it works as you remove each bit. That should at least help you isolate the problem.

Let me know how you get on.

oyeanuj commented 6 years ago

It worked once I upgraded to Rails 5.2 ¯_(ツ)_/¯

Thanks for your help!

brendon commented 6 years ago

SO weird! :D Glad you got it to work :)