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
Why alias_method fails in Rails model
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 :name, :langEN fails with your "undefined method" error. Doing the aliasing explicitly:
works because the
langENmethod will be created (by
method_missing) the first time you try to call it.
which you can use instead:
Allows you to make aliases for attributes, which includes getter, setter, and query methods.
alias_attribute :name, :langEN
method_missingwill know about aliases registered with
alias_attributeand 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
It's a bit misleading that the error appears when running
# do something
skip_store_location. I assume that happens because
skip_store_locationloads several controllers, and one of them is a subclass of
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:
With the module
Sinatra_Base_Patchcontaining the function that overwrites the original.
The example I followed is this:
super + ' World'
prepend FooExtensions # the only change to above: prepend instead of include
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.
To alias an instance method defined on the singleton class you can either open it up using the
method_1 = MyClass.method(:a).unbind
method_2 = MyClass.singleton_class.instance_method(:a)
method_1 == method_2
class << objectsyntax.
Or you can refer to it directly using the
class << MyClass
alias_method :b, :a
#=> "Hello World!"
If you are still within the class context
MyClass.singleton_class.alias_method :c, :a
#=> "Hello World!"
selfwill refer to the class. So the above could also be written as:
class << self
alias_method :b, :a
Or a combination of the two.
singleton_class.alias_method :c, :a
What's The Best Way to Extract Excel Cell Comments Using Perl or Ruby
Can't Install Debugger Gem - Rails - MAC Osx Mavericks
Clean Install Osx 10.9.1 Returns "Undefined Method 'Path2Class'" When Trying to Install Gems
Rake Cucumber and Rake Spec Always Use "Develop" Environment
Actionmailer Pass Local Variables to The Erb Template
How Does Ruby's Sort Method Work with The Combined Comparison (Spaceship) Operator
Recommended Way to Generate a Presigned Url to S3 Bucket in Ruby
Broken Rails Routes After Implementing Single Table Inheritance
Rails for Zombies Lab 4 > Exercise 3
How to Install Wraith on Windows 7
Can Optionparser Skip Unknown Options, to Be Processed Later in a Ruby Program
Rails Has_Many Through Form with Additional Attributes
Using Paperclip Within Seeds.Rb
Rails/Postgres, 'Foreign Keys' Stored in Array to Create 1-Many Association
Warning While Installing The Rails Plugin
Explanation of Ruby Code for Building Trie Data Structures
Why Am I Getting "Unable to Autoload Constant" with Rails and Grape