Why do I get an undefined method when calling model method - ruby-on-rails

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

Related

Can't access to attr_accessor in Concern Ruby on Rails

I want to register the class method in concern and access to attr_accessor, but it doesn't work. This is my sample code. Please help me how can I do this. Thank you so much!
app/controllers/concerns/foobar_concern.rb
module FoobarConcern
extend ActiveSupport::Concern
included do
class << self
attr_accessor :foo
end
end
class_methods do
def test_method(bar)
self.foo = bar
end
end
end
app/controllers/foobar_controller.rb
class FoobarController < ApplicationController
include FoobarConcern
test_method 'Just test'
def index
self.foo => NoMethodError: undefined method "foo"
foo => NameError: undefined local variable or method "foo"
end
end
Just delegate required methods to the class like this
module FoobarConcern
extend ActiveSupport::Concern
included do
delegate :foo, :foo=, to: :class
class << self
attr_accessor :foo
end
end
end
The issue is that you're defining a method at the class level (FoobarController.foo) but calling it on an instance of the class (FoobarController.new.foo).
One option is to call the foo method on the class instead:
def index
self.class.foo
end
You can also define an accessor method for instances of the class like:
module FoobarConcern
extend ActiveSupport::Concern
included do
class << self
attr_accessor :foo
end
end
class_methods do
def test_method(bar)
self.foo = bar
end
end
# -- NEW ---
# This `foo` method is defined for instances of the class and calls the class method.
def foo
self.class.foo
end
end

How can I include the module with class to another class?

I have module with class
module My < Grape::API
prefix 'api'
format :json
class Users
helpers do
include My2 #I want put here method 'some_method'
end
end
end
I have another module
module My2
class Circle
def some_method
"Hello"
end
end
end
I can do this, but I wonder how to do with the class
module My2
def some_method
"Hello"
end
end
I don't understand logical .. Thank you
How can I do it another way?
You can't use include for including a class, because it uses for including methods from modules in classes.
If you want to use a class, try to pass an instance of class or create class's method.
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:0x1e120>
Example
I can put
def some_method(x, y)
My2::Circle.new.some_method(x, y)
end

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

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"

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