Ruby on rails 如何创建此模型元方法?

Ruby on rails 如何创建此模型元方法?,ruby-on-rails,ruby,ruby-on-rails-4,methods,Ruby On Rails,Ruby,Ruby On Rails 4,Methods,我曾经有过下面的模型方法,我使用if@invitation.invite\u expired: def invite_expired? cycle_invite_sent_at < 2.hours.ago end 有人知道我做错了什么吗?你应该像这样使用它: if @invitation.expired?("cycle") # some code here. 我假设这是因为控制器代码中出现了未定义的局部变量或方法“cycle”,当您尝试使用普通cycle而不是字符串“cycle

我曾经有过下面的模型方法,我使用
if@invitation.invite\u expired

def invite_expired?
  cycle_invite_sent_at < 2.hours.ago
end

有人知道我做错了什么吗?

你应该像这样使用它:

if @invitation.expired?("cycle")
  # some code here.
我假设这是因为控制器代码中出现了
未定义的局部变量或方法“cycle”
,当您尝试使用普通
cycle
而不是字符串
“cycle”
时,就会发生这种情况

它说“未定义的局部变量或方法‘cycle’”,所以第一个问题是:是否有一个局部变量或方法名为
cycle
?如果没有,您需要定义它。如果有,下一个问题是:它定义在什么范围内,您试图在什么范围内使用它?
NameError: undefined local variable or method `cycle' for #<InvitationsController:0x0000000a5e8d50>
if @invitation.expired?("cycle")
  # some code here.