如何通过ID查找记录并选择几列,在Rails上使用ruby? [英] How to find a record by id and select few columns, ruby on rails?

查看:69
本文介绍了如何通过ID查找记录并选择几列,在Rails上使用ruby?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个User模型,该模型具有所有查询字段以及数据库中的现有数据.当我执行以下查询时-

I have a User model that has all the queried fields with the existing data in the database. When I execute the following query-

@user = User.find(4, :select => 'user_fname, user_lname')

Rails在上一行中引发以下错误

Rails throws the following error for the above line

无法找到所有具有'user_id'的用户:(4,{:select =>"user_fname,user_lname"})(找到1个结果,但正在寻找2个结果)

Couldn't find all Users with 'user_id': (4, {:select=>"user_fname, user_lname"}) (found 1 results, but was looking for 2)

怎么了?

推荐答案

您可以尝试一下.我希望这会有所帮助.

You can try this. I hope this will help.

@user = User.where("id = ?", 4).select( "user_fname, user_lname")

这篇关于如何通过ID查找记录并选择几列,在Rails上使用ruby?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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