Rails: Undefined Method Error for Namespaced, STI-Inherited Class - ruby-on-rails

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?

Related

Rails NoMethodError: undefined method `name' for #<JobClass

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.

Creating dynamic instance methods in after_save callback rails

I have a after_save callback in a model named Field and i am creating dynamic instance methods in it on other model named User, but the code is not working, i am unable to figure out whats wrong with it, as the logic is very simple.Please help.
class field < ActiveRecord::Base
after_create :create_user_methods
private
def create_user_methods
User.class_eval do
define_method(self.name) do
#some code
end
define_method(self.name + "=") do
#some code
end
end
end
end
and then I am creating Field instance in rails console like this
Field.create(name: "test_method")
And then calling that method on User class instance like this
User.new.test_method
But it raises error
undefined method test_method for ....
I got the fix, I can not use self inside the class_eval block as its value is User model not Field class object, therefore the fix is:
class field < ActiveRecord::Base
after_create :create_user_methods
private
def create_user_methods
name = self.name # here self points to field object
User.class_eval do
define_method(name) do
#some code
end
define_method(name + "=") do
#some code
end
end
end
end

Undefined method in controller

I'm getting an error when trying to call a method in my controller. Following tutorials on how to get this to work but just a bit stuck in the mud and need some help.
NoMethodError in CatalogController#index
undefined method `art' for #<Class:0x007fbe8c338310>
My model
require 'httparty'
require 'json'
class Feed < ActiveRecord::Base
include HTTParty
base_uri 'https://www.parsehub.com/api/v2/runs'
# GET /feeds
# GET /feeds.json
def art
response = self.class.get("/tnZ4F47Do9a7QeDnI6_8EKea/data?&format=json")
#elements = response.parsed_response["image"]
#parsed = #elements.collect { |e| e['url'] }
end
end
My controller
class CatalogController < ApplicationController
def index
#images = Feed.art
end
end
I'm guessing it's something fairly simple I'm forgetting.
def art defines an instance method, not a class method.
You have two options to fix this problem:
1) Make the method a class method by adding self. to the definition:
def self.art
# ...
2) Or create a Feed instance in your controller before calling art:
def index
#images = Feed.new.art
end

NoMethodError (undefined method) however class method is defined

I have created a new class Project which is inherited from ActiveRecord::Base. I defined a class method called get_all and I would like to use in Controller but I got NoMethodError (undefined method for ...)
Model:
class Project < ActiveRecord::Base
def self.get_all
find(:all)
end
end
Controller:
class Controller < ApplicationController
unloadable
def index
#projects = Project.get_all
end
end
Note that in rails 3 the find(:all) method ( without any options ) is deprecated in favor of the all method. More about it:
http://m.onkey.org/active-record-query-interface
Also, I don't know why are you making that function, when you could just do:
#projects = Project.all
just like chrisbulmer said.
This should work:
Project model
def self.get_all
Project.all
end

How does one make a Model perform a method on itself?

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

Resources