How does one make a Model perform a method on itself? - ruby-on-rails

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

Related

how to call define_method in rails after_kind hook

when i call define_method in hook function :
error occurred:
undefined method `define_method' for #<Myentity:0x007f9e4eda5928>
here is an example:
class EntityInstance < ApplicationRecord
after_find :define_relation
def define_relation
define_method "example" do |x|
end
end
end
How to change the context in hook method or how to use this function in hook method?
thx a lot!
define_method should be called in the context of the class.
Meaning, depending on where you want to define the method (either in singleton class of the instance of Myentity or in Myentity class) you should be using either
def define_relation
# define method available only to this particular instance of Myentity
class_eval do
define_method "example" do |x|
end
end
end
or
def define_relation
# define a method available to all instances of Myentity class
self.class_eval do
define_method "example" do |x|
end
end
end

Why do I get an undefined method when calling model method

I have a model with the following method
class Legacy::Retailer < ActiveRecord::Base
belongs_to :retailers
self.table_name = 'orim_retailers'
def retailer_options
retailers = Array.new
self.display_name.each do |names|
retailers << names
end
end
end
When I try to call this method from the controller as
#retailer_options = Legacy::Retailer.retailer_options
I get a method an undefined method error. I'm not sure what I'm doing wrong.
Because you defined an instance method, not a class method.
To be able to call
Legacy::Retailer.retailer_options
you should define a class method:
def self.retailer_options # note self here
retailers = Array.new
self.display_name.each do |names|
retailers << names
end
end
And if you've meant to call this method on instances, not class itself, you should be able to do the following:
Legacy::Retailer.new.retailer_options
The method retailer_options was defined as an instance method, and you are calling a class method. Take a look at the following code:
class Foo
def self.bar
puts 'class method'
end
def baz
puts 'instance method'
end
end
If you call:
Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class
And for the instance method:
Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e820>
Hope it's more clear now
you've defined an instance method but you're calling it as a class method.
#retailer = Legacy::Retailer.new
#retailer.retailer_options
will work

undefined method `get_category' for TestObserver:Class

In TestObserver class, I have self.delivered_email(message) method, the message is the action mailer instance, I'm calling get_category method in self.delivered_email method. But I received error message "undefined method `get_category' for TestObserver:Class". What is the problem and how could I solve it? I'm using observer pattern here in rails.
class TestObserver
def self.delivered_email(message)
begin
category = get_category(message)
# do something here
rescue => ex
# do something here
end
end
private
def get_category(message)
# do something here
end
end
ActionMailer::Base.register_observer(TestObserver)
This is because, you are trying to access a instance method from a class method
your self.delivered_email is a class level method, if it calls other methods those should be class methods too. In this case get_category(message) is a instance method.
To fix the error you could make the get_category(message) as a class method. (if it fits your context)
class TestObserver
def self.delivered_email(message)
begin
category = get_category(message)
# do something here
rescue => ex
# do something here
end
end
def self.get_category(message)
# do something here
end
end

undefined method when calling a method in a model of Rails

I keep getting undefined method for when I call a certain method from my Model.
class User < ActiveRecord::Base
def update!
request_info
end
def request_info
return "hmmm"
end
end
request_info inside of update! is not defined
I've tried making it self.request_info as well but that doesn't work either
There are two ways to call a method in rails.
class Foo
def self.bar
puts 'class method'
end
def baz
puts 'instance method'
end
end
Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class
Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e820>
Are you doing the same? I have taken this example from here. Take a look at that page for details.
update! is a bad choice for a method name: update is already defined as a (private) method on ActiveRecord::Base - that might lead to confusion.
>> u = User.last
>> u.update
NoMethodError: private method `update' called for #<User:0x007ff862c9cc48>
but apart from that you code works perfectly fine when I try it in the console:
>> u = User.last
>> u.update!
=> "hmmm"

define method in model that can be accessed in controller

I have defined a problems method in my Report model. I need to use the value of Report.problem in the report's controller while defining the action show. But i keep getting the error message 'undefined method problem'. How do i solve this? Any assistance would be greatful.
I have a report model and a problem model that contains a list of all problems.
In report model
def problems1
Problem.find(:all, :conditions => )
end
In the reports controller i need something like
def show
#report = Report.problems1
end
you have to assign self.method_name to use as a class method
Follow following rule for Model methods
Class Method
def self.problem
end
in controller
Report.problem
Instance method
def problem
end
in controller
report = Report.new
report.problem
If you define method as class method
class Report < ActiveRecord :: Base
def Report.problem
puts 1
end
end
Report.problem
>1
But if you define method as object
class Report < ActiveRecord :: Base
def problem
puts 1
end
end
This method call
report = Report.new
report.problem
>1

Resources