Alias_Method on Activerecord::Base Results in Nameerror

alias_method on ActiveRecord::Base results in NameError

According a site I found, you're supposed to use alias_attribute instead:

The problem is that ActiveRecord doesn't create the accessor methods
on the fly until the database connection is live and it has parsed the
table schema. That's a long time after the class has been loaded.

class LeadImport::Base < ActiveRecord::Base
alias_attribute :address_1, :address_line_1
end

Why alias_method fails in Rails model

ActiveRecord uses method_missing (AFAIK via ActiveModel::AttributeMethods#method_missing) to create attribute accessor and mutator methods the first time they're called. That means that there is no langEN method when you call alias_method and alias_method :name, :langEN fails with your "undefined method" error. Doing the aliasing explicitly:

def name
langEN
end

works because the langEN method will be created (by method_missing) the first time you try to call it.

Rails offers alias_attribute:

alias_attribute(new_name, old_name)

Allows you to make aliases for attributes, which includes getter, setter, and query methods.

which you can use instead:

alias_attribute :name, :langEN

The built-in method_missing will know about aliases registered with alias_attribute and will set up the appropriate aliases as needed.

After initializer change 'alias_method' undefined method 'current_user' for class 'ApplicationController'

The problem is that you use alias_method before the method is defined in ApplicationController. To fix the problem, move the line

alias_method :devise_current_user, :current_user

below

def current_user
# do something
end

It's a bit misleading that the error appears when running skip_store_location. I assume that happens because skip_store_location loads several controllers, and one of them is a subclass of ApplicationController.

Trying to alias old method and create new one in ruby

I found a good answer to my problem here at this link

Instead of monkey patching the original class I instead create a module and then use prepend and just call super to call the original method.
I now do this:

Sinatra::Base.prepend Restman::Patches::Sinatra_Base_Patch

With the module Sinatra_Base_Patch containing the function that overwrites the original.

The example I followed is this:

class Foo
def bar
'Hello'
end
end

module FooExtensions
def bar
super + ' World'
end
end

class Foo
prepend FooExtensions # the only change to above: prepend instead of include
end

Foo.new.bar # => 'Hello World'

Is there an alias_method for a class method?

alias_method aliases an instances method of the receiver. Class methods are actually instance methods defined on the singleton class of a class.

class MyClass
def self.a
"Hello World!"
end
end

method_1 = MyClass.method(:a).unbind
method_2 = MyClass.singleton_class.instance_method(:a)

method_1 == method_2
#=> true

To alias an instance method defined on the singleton class you can either open it up using the class << object syntax.

class << MyClass
alias_method :b, :a
end

MyClass.b
#=> "Hello World!"

Or you can refer to it directly using the singleton_class method.

MyClass.singleton_class.alias_method :c, :a

MyClass.c
#=> "Hello World!"

If you are still within the class context self will refer to the class. So the above could also be written as:

class MyClass
class << self
def a
"Hello World!"
end
alias_method :b, :a
end
end

Or

class MyClass
def self.a
"Hello World!"
end
singleton_class.alias_method :c, :a
end

Or a combination of the two.



Related Topics



Leave a reply



Submit