相关疑难解决方法(0)

为什么alias_method在Rails模型中失败

class Country < ActiveRecord::Base

  #alias_method :name, :langEN # here fails
  #alias_method :name=, :langEN=

  #attr_accessible :name

  def name; langEN end # here works
end
Run Code Online (Sandbox Code Playgroud)

在第一次呼叫alias_method失败时:

NameError: undefined method `langEN' for class `Country'
Run Code Online (Sandbox Code Playgroud)

我的意思是,当我这样做时,它失败了Country.first.

但是在控制台中我可以Country.first.langEN成功调用,并看到第二个调用也可以.

我错过了什么?

ruby activerecord ruby-on-rails alias-method rails-activerecord

24
推荐指数
1
解决办法
8605
查看次数