Ruby on Rails中使用弹拨值 [英] Using Plucked Values in Ruby on Rails

查看:226
本文介绍了Ruby on Rails中使用弹拨值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个名为相册样板,我想用我掐了值。首先,我想尝试打印值。我怎样才能做到这一点?这是我目前的code:

Given a model named Album, I want to use the values I pluck. For starters, I want to try to print the values. How can I do this? Here is my current code:

my_arr = Album.where(title: "Greatest Hits", artist: "The Beatles").pluck(:publisher, :year_published)

puts my_arr[0][0].to_a

为了让事情更简单,我怎么会检索任何价值可言,这样我就可以把它变成一个正常的变量?

To make things even simpler, how would I retrieve any value at all, so that I could place it into a normal variable?

我用Rails 4 sqlite3的。

I'm using Rails 4 with SQLite3.

推荐答案

勇气 已经返回数组 - 你不需要操作的输出

pluck already returns an array - you do not need to manipulate the output of

Album
  .where(title: "Greatest Hits", artist: "The Beatles")
  .pluck(:publisher, :year_published)

以任何方式 - 它是一个数组。在这种情况下,2个元素的数组的数组

in any way - it is an array. In this case an array of 2-element arrays.

如果你有一个空数组 - 我保证你只是没有满足其中,条件的任何记录。这可能是一个小错字在标题结果的或艺术家的名字损失(这开始被无关的问题)。如果你确信你有符合过滤数据库对象 - 一定要仔细检查是否有错别字,因为你的查询是正确的。

If you have an empty array - I guarantee you just do not have any records meeting the where conditions. It might be a result of a tiny typo in title's or artist's name whatsoever (which starts to be unrelated to question). If you are sure you have database objects that meet the filter - make sure to double check for typos, because your query is correct.

这篇关于Ruby on Rails中使用弹拨值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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