Trying to perform a job after saving a record.
Here is my code:
/app/models/some_todo_model.rb
class SomeTodoModel < ApplicationRecord
belongs_to :user
after_save :create_job_for_notificate_on_due_date
def create_job_for_notificate_on_due_date
EmitsNotificationsJob.set(wait_until: self.due_date).perform_later()
end
end
/app/jobs/emits_notifications_job.rb
class EmitsNotificationsJob < ApplicationJob
queue_as :default
# discard_on ActiveJob::DeserializationError
def perform()
end
end
EmitsNotificationsJob.set(wait_until: self.due_date) is setting it well.
But when I am calling perform_later I have the following error:
NoMethodError: undefined method 'name' for #<EmitsNotificationsJob:0x000056387e74ce78>
I don't understand my issue since I am following the documentation here.
As it might be for the same reason, if I am uncommenting the discard_on line, I am having the following error:
NoMethodError: undefined method 'discard_on' for EmitsNotificationsJob:Class
UPD You have copied wrong example(missed .class after job):
class ApplicationJob
before_enqueue { |job| $statsd.increment "#{job.class.name.underscore}.enqueue" }
end
Also, I strongly suggest to remove before_enqueue line from app/jobs/application_job.rb at all. It's just example of before_enqueue callback and the example looks like non-working.
Related
In Rails, is it possible to call methods from the class that included the concern, in the concern itself ? ie:
class Foo < ApplicationRecord
include Encryptable
def self.encrypted_attributes
%i[attr_1 attr_2]
end
end
module Encryptable
extend ActiveSupport::Concern
included do
self.encrypted_attributes do |attr|
define_method("#{attr}=") do |arg|
# do some stuff
end
define_method("#{attr}") do
# do some stuff
end
end
end
end
The issue is, when I try to do that, I get an error like :
*** NoMethodError Exception: undefined method 'encrypted_attributes' for #<Class:0x00005648d71c2430>
And, when debugging inside the concern, I get this something like this :
(byebug) self
Foo (call 'Foo' to establish a connection)
(byebug) self.class
Class
Ruby is a scripting language and the order matters. The following would do:
class Foo < ApplicationRecord
def self.encrypted_attributes
%i[attr_1 attr_2]
end
# OK, now we have self.encrypted_attributes defined
include Encryptable
end
More info: ActiveSupport::Concern#included.
I have a model that extends ActiveRecord::Base and includes a concern:
class User < ActiveRecord::Base
include UserConcern
def self.create_user()
...
results = some_method()
end
end
UserConcern is stored in the concerns directory:
module UserConcern
extend ActiveSupport::Concern
def some_method()
...
end
end
I am getting a run-time error when I try to create a new user by calling the create_user method that looks like this:
undefined method 'some_method' for #<Class:0x000000...>
I have two questions about this:
Why is the some_method undefined? It seems to me that I am properly including it with the statement include UserConcern. Does it have something to do with my User class extending ActiveRecord::Base? Or maybe something to do with the fact that I am calling some_methods() from a class method (i.e. self.create_user())?
Why does the run-time error refer to #<Class:0x000000...> instead of to #<User:0x000000...>?
try it
models/concerns/user_concern.rb:
module UserConcern
extend ActiveSupport::Concern
def some_instance_method
'some_instance_method'
end
included do
def self.some_class_method
'some_class_method'
end
end
end
models/user.rb:
class User < ActiveRecord::Base
include UserConcern
def self.create_user
self.some_class_method
end
end
rails console:
user = User.new
user.some_instance_method
# => "some_instance_method"
User.some_class_method
# => "some_class_method"
User.create_user
# => "some_class_method"
http://api.rubyonrails.org/classes/ActiveSupport/Concern.html
I have an after_create callback in my Tag model:
def auto_vote
params = parametrize_media_tag(media_tag)
Tag::Vote.cast_vote(params)
end
Which gives me this error:
undefined method `cast_vote' for #<Class:0x7ae7a90>
My Tag::Vote model is quite simple:
class Tag::Vote < Vote
def self.cast_vote(params)
Vote.cast_vote_of_type(params, self.class.name)
end
end
Why isn't Rails detecting the cast_vote method?
class MyAwesomeClass
def foobar
puts "trip!"
end
So that I can perform :
MyAwesomeClass.foobar
=> "trip!"
I keep getting :
NoMethodError: undefined method `foobar' for MyAwesomeClass:Class
class MyAwesomeClass
def self.foobar
puts "trip!"
end
end
Using "self" makes the method a class instance method
I just stumbled over a weird problem, and I don't really understand what is causing this.
In our rails app, let's have a mixin Mixin:
module Mixin
def foo
with_scope :find => ... do
...
end
end
end
which is includeed into a model class elsewhere:
class Model < ActiveRecord::Base
include Mixin
...
end
calling Model.new.foo results in an error: NoMethodError: undefined method with_scope
I then changed the foo method to:
def foo
self.class.with_scope :find => ... do
...
end
end
But this also results in an error: NoMethodError: protected method with_scope called
This seems odd. I would have expected that the mixin methods would behave like any other method in Model. I never stumbled over this before, because all the instance methods like save are there and work as usual.
Am I doing it all wrong?