Ruby on rails 获取意外的“,”,预期=>ban错误

Ruby on rails 获取意外的“,”,预期=>ban错误,ruby-on-rails,ruby,activerecord,rails-activerecord,Ruby On Rails,Ruby,Activerecord,Rails Activerecord,我从Ruby得到这个错误: `语法错误,意外的“,”,预期=>ban=ban。其中:BANCED=>1,过期时间符号,可以吗?@aidiah:只使用where和first。但是我仍然得到存在的错误是未定义的。但是我不能在查找中使用>符号,可以吗?@aidiah:只使用where和first

我从Ruby得到这个错误:

`语法错误,意外的“,”,预期=>ban=ban。其中:BANCED=>1,过期时间<?,当前时间^

我也得到了这个错误:

1的未定义方法“过期”

这是我的代码:

class Ban < ActiveRecord::Base
  before_create :unban
  def unban
    puts "starting unban"
    current_time = Time.now
    puts current_time
    ban = Ban.where(:banned => 1, "expires < ?", current_time)
    if current_time > ban.expires
      ban.update_attributes(:banned => 0)
      puts "worked"
    end
  end
end

您应该使用字符串或哈希表示法,即:

Ban.where(banned: 1).where('expires < ?', current_time) 

另外,当您使用where时,它会返回一组记录,而不是一个Ban实例,因此请使用Ban.first.expires不要忘记,结果可能为零,您可能也应该检查它

您应该使用字符串或哈希表示法,即:

Ban.where(banned: 1).where('expires < ?', current_time) 

另外,当您使用where时,它会返回一组记录,而不是一个Ban实例,因此请使用Ban.first.expires不要忘记,结果可能为零,您可能也应该检查它

您混合了where子句条件类型;一个是散列条件,另一个使用替换参数。您可以使用更短的方法,它使用单个字符串,如下所示:

ban = Ban.where("banned = 1 and expires < ?", current_time)
ban = Ban.where(banned: 1).where("expires < ?", current_time)
或者将查询结果和if语句更改为:

ban = Ban.where("banned = 1 and expires < ?", current_time).first
if ban && current_time > ban.expires

您混合了where子句条件类型;一个是散列条件,另一个使用替换参数。您可以使用更短的方法,它使用单个字符串,如下所示:

ban = Ban.where("banned = 1 and expires < ?", current_time)
ban = Ban.where(banned: 1).where("expires < ?", current_time)
或者将查询结果和if语句更改为:

ban = Ban.where("banned = 1 and expires < ?", current_time).first
if ban && current_time > ban.expires

但是我仍然得到存在的错误是未定义的。但是我不能在查找中使用>符号,可以吗?@aidiah:只使用where和first。但是我仍然得到存在的错误是未定义的。但是我不能在查找中使用>符号,可以吗?@aidiah:只使用where和first