I'm working with Spree 2.1 and trying to add new payment gateway, but this error is more generic so Spree itself is not-so-important here.
I have encountered that error (undefined method 'association_class' for nil:NilClass) after adding some module to Spree::PaymentMethod (source) class:
spree/payment_method_decorator.rb
Spree::PaymentMethod.class_eval do
include Spree::Core::CalculatedAdjustments
end
(Spree::Core::CalculatedAdjustments source)
(Spree::Gateway source)
Unfortunately, now Spree::PaymentMethod (source) breaks a little, i.e:
n = Spree::PaymentMethod.first
=> #<Spree::Gateway::Bogus id: 1, (...)>
n.save
=> undefined method 'association_class' for nil:NilClass
n.calculator
=> undefined method 'association_class' for nil:NilClass
Does anyone know why this happens and how to fix it?
In fact I already have an answer (after few hours of struggle), but maybe someone will give a better one with proper explanation. Maybe the answer is quite obvious, but it wasn't for me and I couldn't find anything related on SO so hopefully someone else with similar level of RoR knowledge won't have to spend another few hours on that.
So the answer was:
As it is visible in above links, Spree::Gateway is inheriting from Spree::PaymentMethod and my custom payment method was inheriting from Spree::Gateway class - something like:
module Spree
class Gateway::CustomMethod < Gateway
end
end
All I had to do was to include Spree::Core::CalculatedAdjustments in Spree::Gateway:
Spree::Gateway.class_eval do
include Spree::Core::CalculatedAdjustments
end
and it is working since then.
Related
We are getting this error on all local setups of our project(currently) when we call any method like:
belongs_to :abc
after_create :some_method
def some_method
if self.abc.saved_change_to_parent_id?
...
// or even self.abc.parent_id_before_last_save
end
It gives:
NameError (undefined local variable or method `first' for ActiveRecord::NullMutationTracker:Class):
app/models/model_name.rb:50:in `some_method'
Yes, there is not a full trace with rails internal file paths etc, i only get my project files trace. Maybe its some logger config issue, any help to get full trace will also be appreciated.
There are no such issues i could find on internet, thats why posting here.
PS: Not posted on rails issue tracker(github issues) because i don't have minimal reproduction.
Stack
ruby: 2.4.3
rails: 5.1.5 (also tried on 5.1.7)
OS: Ubuntu 20, also tried on macOS
Thanks in advance.
UPDATE1: Using byebug, i got to this trace, where error is occuring:
/Users/dev/.rvm/gems/ruby-2.4.3/gems/acts_as_singleton-0.0.8/lib/acts_as_singleton.rb:43
which is here, so it is not directly from rails, but a very outdated gem we have in our code for some reason.
As i posted in the update, issue was not with rails or anything, i used byebug to track trace and it was something like:
activemodel-5.1.5/lib/active_model/attribute_methods.rb:384
activerecord-5.1.5/lib/active_record/attribute_methods/dirty.rb:146
activerecord-5.1.5/lib/active_record/attribute_methods/dirty.rb:319
activerecord-5.1.5/lib/active_record/attribute_methods/dirty.rb:315
acts_as_singleton-859f49112c03/lib/acts_as_singleton.rb:43
The last line, should not be there, as we are not using acts_as_singleton on any of the involved models.
So after some tracking, it turned out issue with the gem. I have created a fork and used that fork. Here is the fork: https://github.com/ziaulrehman40/acts_as_singleton (forked it from another fork, which seemingly had some other fixes as well). And you can see my changes here.
What was the issue?
This gem writes a module named Singleton within ActiveRecord module. Which seems ok, unless you realize that there is another module named Singleton already. Whihc is being included in:
gems/activerecord-5.1.5/lib/active_record/attribute_mutation_tracker.rb:83
class NullMutationTracker # :nodoc:
include Singleton
...
So as you can see, this Singleton module gets overriden(or expanded, not sure) un-intentionally by that outdated gem(acts_as_singleton).
I'm trying to use modules for namespacing reasons. I have this file located in my Rails app at /lib/reports/stripe.rb.
module Reports
module Stripe
def self.foo
puts 'i am foo'
end
end
end
In my console, I'd expect to be able to call foo by Reports::Stripe.foo or Reports::Stripe::foo, but I get the error
NoMethodError: undefined method `foo' for Reports::Stripe:Module
What am I doing wrong? Also feel free to let me know if there's a better way to organize the location and namespacing.
All method calls in ruby use the . syntax. Even "module" methods.
> Reports::Stripe.foo
i am foo
You may be receiving the error NoMethodError: undefined method 'foo' for Reports::Stripe:Module if you have added the method after you have started the rails console. Try restarting the console or reloading the file with load 'reports/stripe'.
The file was actually located in /lib/reports/stripe/stripe.rb. It was a mistake I made much earlier, but forgot to fix. Moving the file to /lib/reports/stripe.rb resolved the issue.
I am finishing the book and I've ignored this error message for too long. Please help me understand how to fix this. Thank you!
1) Error:
PasswordResetsTest#test_password_resets:
NoMethodError: undefined method reset_sent_at=' for #<User:0x007f814e118600>
app/models/user.rb:63:increate_reset_digest'
app/controllers/password_resets_controller.rb:12:in create'
test/integration/password_resets_test.rb:17:inblock in '
The error might point you a little bit into the wrong direction. This NoMethodError is actually caused by that fact that your users table doesn't have a column reset_sent_at.
Rails defines accessor methods (getters and setters) on your User model for each column in the users table. However, since it doesn't have a reset_sent_at column, no accessor is defined and the NoMethodError is being raised.
Make sure you've created the migration where reset_sent_at was added to users (this was done in chapter 10 of the Rails tutorial), and that you have executed the migration as well. After the migration, be sure to restart your Rails server.
I'm using the latest version of the Impressionist and Rails Admin gems, and wondering if anyone could shed some light on an annoying conflict I'm experiencing. The problem is roughly documented here - https://github.com/sferik/rails_admin/issues/1315, yet the vaguely described solution is not working for me. When I have the line is_impressionable in my Listing model, I get an error when starting my Rails server with rails s:
...rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined local variable or method `is_impressionable' for Listing(no database connection):Class (NameError)
If I first start the server, and then add the 'is_impressionable' line, everything works fine, so the problem only occurs during initialization. I don't fully understand the initialization process, so am not sure how to go about getting this to work.
I have tried moving all my rails_admin model configuration options to their respective models, rather than in the initializer, which had no effect. I also have the following line in my initializer:
config.included_models = [Listing,ListingImage,AllOtherModelsHere...]
I have tried adding single quotes around these model names, which results in the following errors, as described in the github issue here
[RailsAdmin] Could not load model Listing, assuming model is non existing. (undefined local variable or method `is_impressionable' for Listing(no database connection):Class)
Any ideas what else I can try to make these gems work together? I don't want to have to remove the is_impressionable line every time I want to restart the server or generate a migration...
Not sure if the same issue that I had but yet I will post what worked for me just in case someone struggles with this too:
Im on a ruby 2.1.5 project with rails 4.2.0 and among other gems I'm using rails admin.
I run into several weird problems trying to set this up. For instance if I added the is_impressionable call within one of my models for some reason the execution of that file stopped there and I started getting weird errors like any method declared below the is_impressionable failed with undefined error.
So what I end up doing was:
class MyModel < ActiveRecord::Base
include Impressionist::IsImpressionable
is_impressionable
end
So this solved my issue and now i can access #my_model_instance.impression_count as expected.
I changed every occurrence of Klass to 'Klass'.constantize in initializer.
I've inherited a project in which an ActiveRecord model is giving me strange behaviour. This is the only model that's behaving this way, so far.
car = Car.first
car.respond_to?(:each) # true
There is no each method defined in Car that I can find.
I read in another post that you can find out who injected the method, so I gave it a shot:
car.method(:each) # raises "undefined method `each' for class `Car'
I want to use Car with draper but because my instance behaves like an Enumerable, draper treats it as such. Draper is not the issue, however.
What should I do to figure it out?
The comments to my question helped me find it! I used Dmitry's binary-search to figure out what caused it, and found the problem in a gem that the model is using. The gem, Mark Reed, overrides respond_to. So the lesson is: if responds_to is funky, perhaps it's being overridden somewhere.