问题标签 [pluck]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
1487 浏览

ruby-on-rails - 在 Ruby on Rails 中使用 Plucked 值

给定一个名为 Album 的模型,我想使用我采摘的值。对于初学者,我想尝试打印这些值。我怎样才能做到这一点?这是我当前的代码:

为了让事情变得更简单,我将如何检索任何值,以便我可以将它放入普通变量中?

我正在使用带有 SQLite3 的 Rails 4。

0 投票
1 回答
179 浏览

ruby-on-rails - Pluck doesn't return a bother records they have the same name

I have a pluck that is turned into a hash and stored in a variable

If 2 records have the same :key name then only the most recent record is used and they aren't both added to the hash. But if they have the same value and different keys both are added to the hash.

This also occurs if I swap :key and :value around (.pluck(:value, :key)). If they have now the same value it only uses the most recent one and stores that in the hash. But having the same key is now fine.

I'm not sure of this is being caused by pluck or from being sorted in a hash. I'm leaning towards pluck being the culprit.

What is causing this and how can I stop it from happening. I don't want data being skipped if it has the same key name as another record.

0 投票
2 回答
657 浏览

forms - 轨道,表单选择,采摘 2 个字段

我试图在表单选择(f.select)中简单地显示 2 个字段(名字和姓氏)。这是我的代码:

使用上面的代码,选择下拉字段只显示名字。我正在使用 Active Record 4.2 和 Rails 4。有什么帮助吗?

0 投票
1 回答
355 浏览

ruby-on-rails - 如何仅使用 .pluck 来获取前 x 条记录?

.pluck用于仅获取前 5 个值

例如

我只想要前 5 个名字。此外,如果只有 3 个名称,它不会抛出错误,它只会抓取 3 个。

在文档或谷歌搜索中看不到它。我将如何做到这一点?

0 投票
1 回答
6198 浏览

ruby-on-rails - rails4 采摘顺序和限制

在我的侧边栏中,我显示了新创建的用户配置文件。配置文件belongs_to用户和用户has_one_profile。我意识到我只使用了配置文件表中的 3 列,所以使用 pluck 会更好。我也有一个link_to user_path(profile.user)部分,所以我必须以某种方式告诉用户是谁。目前我正在使用includes,但我不需要整个用户表。所以我使用了来自用户和配置文件表的许多列。

我怎样才能用 pluck 优化它?我尝试了几个版本,但总是出现一些错误(大部分时间 profile.user 未定义)。

我当前的代码:

0 投票
1 回答
214 浏览

forms - Laravel 表单使用 $key => $value ($value2) 代替 $key => $value

对不起,如果标题误导...

我有以下使用 laravel Form 门面创建一个选择框:

现在$catgories_selectidnamepluck()_

我想对另一个选择(税收规则)做同样的事情,但我想像下面这样:

<option value="id">$value1 ($value2)</option>

我怎样才能做到这一点?

0 投票
4 回答
27072 浏览

php - Laravel 使用 concat 和 pluck 方法

我正在使用 Laravel.5.3,以下是我的查询

这会引发一个错误

isset 中的非法偏移类型或为空

我可以知道这是否是正确的方法吗?

如果我不使用联系方式并使用 like

这是正确的并给了我结果

预期结果 :

其中名字和姓氏连接在一起。

0 投票
1 回答
27 浏览

pluck - 如何选择具有数组中值的属性?

在这里,我有一个包含许多对象的数组。在那我有一个字段作为“id”。我只想要值为 null 的 id。怎么选?

那么如何采摘价值呢?

0 投票
4 回答
4639 浏览

php - 在没有关联数组的情况下使用 Laravel 的 pluck 方法

假设我有一个Article收藏,我想获取热门文章。

所以首先,我在scopePopular()方法中做了一个Article方法。很简单。

现在我想获取他们id的 s,所以我会这样做:

结果将是一个关联数组:

我想得到一个没有键的普通数组,比如:

我知道我可以做这样的事情:

但我相信 Laravel 有更清洁的方法来做到这一点。有任何想法吗?

0 投票
1 回答
125 浏览

ruby-on-rails - 使用 Wikipedia-Client Gem 更新 Rails 数据库

我的 ruby​​ 和 Rails 有点生锈了。我的数据库中有一个名为的表,institutes其中填充了一些列。我想使用 Wikipedia-Client gem 来填充其他一些。我想使用该name属性在 Wikipedia 上查找页面,然后page.summary用于description我的表page.image_urls.first中的属性和picture属性。目前,我正在努力弄清楚我将如何解决这个问题。

我目前的代码是:

我在这里显然做错了选择和使用名称属性来查找维基百科页面,但不能完全解决。我想即使我正确地提取了这个名字,它也不会为正确的 id 分配任何东西。

如果还有一种方法可以在 Wikipedia 搜索中删除名称开头的“The”(如果它存在于 中:name),那也会很有帮助,因为似乎有些机构将其放在 Wikipedia 上。