语法错误:(irb):26:给定了块arg和实际块 [英] SyntaxError: (irb):26: both block arg and actual block given

查看:72
本文介绍了语法错误:(irb):26:给定了块arg和实际块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个查询

= f.select(:city, Country.where(:country_code => "es").collect(&:cities) {|p| [ p.city, p.id ] }, {:include_blank => 'Choose your city'})

问题是我遇到以下错误

SyntaxError: (irb):26: both block arg and actual block given

从我所看到的m通过包含 collect(&:cities)然后声明该块来做错了事。

From what I see I'm doing something wrong by including the collect(&:cities) and then declaring the block. Is there a way I can accomplish both with same query?

推荐答案

Country.where(:country_code => "es").collect(&:cities)

完全相同as

Country.where(:country_code => "es").collect {|country| country.cities}

这就是为什么会出现错误的原因:您将两个块传递给 collect 方法。您实际上的意思可能是这样的:

And this is why you are getting your error: you pass two blocks to the collect method. What you actually meant was probably something like this:

Country.where(:country_code => "es").collect(&:cities).flatten.collect {|p| [ p.city, p.id ] }

这将检索国家/地区,并获取城市列表对于每个国家/地区,请将数组展平到只有一个一维数组,然后返回选择的数组。

That will retrieve the countries, get the list of cities for each country, flattens the array to that you only have a one-dimensional one and the returns your array for the select.

由于每个国家/地区可能只有一个国家/地区代码,您也可以这样写:

As there is probably only one country per country code, you can also write it that way:

Country.where(:country_code => "es").first.cities.collect {|p| [ p.city, p.id ] }

这篇关于语法错误:(irb):26:给定了块arg和实际块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆