gpt4 book ai didi

ruby-on-rails - lambda 范围错误内的 Rails 模型 block

转载 作者:数据小太阳 更新时间:2023-10-29 07:37:46 25 4
gpt4 key购买 nike

我想在我的模型范围之一上返回自定义集合..但是我不知道为什么当我在我的 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 |match|
buff_date = match.match_start_time.to_date.to_s
matches[buff_date] ||= []
matches[buff_date] << match
end
matches
end

它将在这一行显示错误:buff_matches.each do |match| 错误信息:SyntaxError: match.rb:15: syntax error, unexpected keyword_do_block, expecting keyword_end.

但是如果我把我的代码改成这样:

scope :date, -> (from_date, to_date) do
buff_matches = where match_start_time: from_date..to_date
matches = {}
buff_matches.each { |match|
buff_date = match.match_start_time.to_date.to_s
matches[buff_date] ||= []
matches[buff_date] << match
}
matches
end

它会正常工作。我想使用 do end 语法,因为它看起来比使用花括号更干净。您知道为什么会发生此错误吗?

最佳答案

您似乎遇到了极端情况。我真的无法解释为什么它会失败,但这修复了它并使用了 do..end block

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

关于ruby-on-rails - lambda 范围错误内的 Rails 模型 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25591399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com