新手问题 Redmine 这个方法没看懂

bevanpf · July 04, 2017 · Last by bevanpf replied at July 04, 2017 · 1556 hits

TimeLogControlle的 new 方法中创建 TimeEntry 时传了一堆参数但是 TimeEntry 类 的 initialize 方法好像并没有对参数进行处理,很迷惑,有知道的或者我哪里搞错了可以指点一下吗,不胜感激~

def new
    @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
    @time_entry.safe_attributes = params[:time_entry]
end
def initialize(attributes=nil, *args)
    super
    if new_record? && self.activity.nil?
      if default_activity = TimeEntryActivity.default
        self.activity_id = default_activity.id
      end
      self.hours = nil if hours == 0
    end
 end
  1. super 代表它使用了父类ActiveRecord::Base#initialize方法;
  2. ActiveRecord::Base中描述了#initialize Methods included from Core;
  3. 具体定义在这里
Reply to flypiggys

Super 也没传参过去吧?另外 super 是 ActiveRecord:;base 还是 SafeAttribute?

Reply to hemengzhi88

好的,我研究下

直接调用 super 是带当前参数的。 super() 这样是显式指定参数的。

Reply to flypiggys

原来如此,多谢~

You need to Sign in before reply, if you don't have an account, please Sign up first.