Ruby-on-rails 在控制台中找到 rails ActiveRecord

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10661586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:25:05  来源:igfitidea点击:

rails ActiveRecord find in console

ruby-on-railsruby-on-rails-3activerecord

提问by user740970

When I'm in the Rails 3.2 console, I can do this just fine:

当我在 Rails 3.2 控制台中时,我可以很好地做到这一点:

p = Person.last
p.last_name

and it prints the last name.

并打印姓氏。

But when I try to find it by the id, it's able to locate the single record and store it in my variable p, but I can't print the last_namecolumn. For example:

但是当我尝试通过 找到它时id,它能够找到单个记录并将其存储在我的变量中p,但是我无法打印该last_name列。例如:

p = Person.where(id: 34).limit(1)

printing phere shows all the columns but p.last_namesays this

p此处打印显示所有列,但这样p.last_name

NoMethodError: undefined method `last_name' for
#<ActiveRecord::Relation:0x000000055f8840>

any help would be appreciated.

任何帮助,将不胜感激。

回答by x1a4

A wherequery will return an ActiveRecord::Relation, which sort of acts like an array, even if you limit the number of returned records.

一个where查询将返回ActiveRecord::Relation,这有点像一个数组的行为,即使你限制返回的记录数。

If you instead change your query to:

如果您改为将查询更改为:

p = Person.where(id: 34).first

it will work as you want, and arel knows to automatically limit the query to a single result, so you don't have to explicitly specify limit(1).

它将按您的需要工作,并且 arel 知道自动将查询限制为单个结果,因此您不必明确指定limit(1).

You could also change to either

您也可以更改为

p = Person.find(34) # Throws an ActiveRecord::RecordNotFound exception if Person with id 34 does not exist

or

或者

p = Person.find_by_id(34) # Returns nil if Person with id 34 does not exist. Does *not* throw an exception.

and it will return a single record as expected.

它将按预期返回单个记录。

EDIT: A where query returns an ActiveRecord::Relation, as @mu is too shortmentioned in comments.

编辑:一个 where 查询返回一个 ActiveRecord::Relation,因为@mu在评论中提到的太短了。

回答by Kevin Bedell

This returns a collection of active record objects:

这将返回活动记录对象的集合:

p = Person.where(id: 34).limit(1)

But there's only one with id = 34, so it's a collection of 1.

但是只有一个id = 34,所以它是1的集合。

The way to do this is:

这样做的方法是:

p = Person.where(id: 34).limit(1).first

or, better:

或更好:

p = Person.where(id: 34).first

or, even better:

或者,甚至更好:

p = Person.find(34)

回答by DVG

What you are probably actually looking for is

您可能实际上正在寻找的是

@person = Person.find(34)
@person.last_name

回答by Sai Ram Reddy

In your case Person is a class and it is inherited from ApplicationRecord

在您的情况下 Person 是一个类,它是从 ApplicationRecord 继承的

p = Person.where(id:10).limit(1)

It returns just the result of the query not the object.

它只返回查询的结果而不是对象。

You can check it by using

您可以使用

p.class # => It reurns Nilclass which means its not at all a class 

So you cannot use p.last_name or p.first_name on p.

所以你不能在 p 上使用 p.last_name 或 p.first_name。