Ruby on rails lambda范围内的Rails模型块错误
我想返回一个模型作用域上的自定义集合。。 但我不知道为什么在lambda范围内使用do end block时会显示错误。。 我正在使用rails 4.1.0和ruby 2.1.2 以下是我的模型中的范围代码:Ruby on rails lambda范围内的Rails模型块错误,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想返回一个模型作用域上的自定义集合。。 但我不知道为什么在lambda范围内使用do end block时会显示错误。。 我正在使用rails 4.1.0和ruby 2.1.2 以下是我的模型中的范围代码: scope :date, -> (from_date, to_date) do buff_matches = where match_start_time: from_date..to_date matches = {} buff_matches.each do |mat
scope :date, -> (from_date, to_date) do
buff_matches = where match_start_time: from_date..to_date
matches = {}
buff_matches.each do |match|
buff_date = match.match_start_time.to_date.to_s
matches[buff_date] ||= []
matches[buff_date] << match
end
matches
end
范围:日期,->(从日期到日期)do
buff\u matches=匹配开始的位置\u时间:从\u日期..到\u日期
匹配项={}
buff|u匹配。每个都匹配|
buff_date=match.match_start_time.to_date.to_s
匹配[buff_date]| |=[]
匹配[buff_date](从_date,到_date)do
buff\u matches=匹配开始的位置\u时间:从\u日期..到\u日期
匹配项={}
buff|u匹配|
buff_date=match.match_start_time.to_date.to_s
匹配[buff_date]| |=[]
匹配[buff_date]看起来您遇到了一个边缘情况。我无法真正解释它失败的原因,但这会修复它并使用do..end块
scope :date, lambda do |from_date, to_date|
buff_matches = where match_start_time: from_date..to_date
matches = {}
buff_matches.each do |match|
buff_date = match.match_start_time.to_date.to_s
matches[buff_date] ||= []
matches[buff_date] << match
end
matches
end
scope:date,lambda do | from|u date,to|u date|
buff\u matches=匹配开始的位置\u时间:从\u日期..到\u日期
匹配项={}
buff|u匹配。每个都匹配|
buff_date=match.match_start_time.to_date.to_s
匹配[buff_date]| |=[]
匹配[buff_date]不幸的是,当我使用这段代码时,我将遇到另一个错误。错误消息是:ArgumentError:尝试创建没有块的Proc对象。我可以通过将lambda像这里的解决方案一样包装在大括号中来克服错误:除非将作用域参数括在括号中,否则不能使用do/end。这里有更详细的说明
scope :date, lambda do |from_date, to_date|
buff_matches = where match_start_time: from_date..to_date
matches = {}
buff_matches.each do |match|
buff_date = match.match_start_time.to_date.to_s
matches[buff_date] ||= []
matches[buff_date] << match
end
matches
end