Ruby-on-rails 检查 ActiveRecord find 是否返回结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2866473/
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-02 22:41:33  来源:igfitidea点击:

Checking if ActiveRecord find returns a result

ruby-on-railsrubyactiverecord

提问by nickcharlton

I'm trying to check if a find method returns a result. My find method is the following:

我正在尝试检查 find 方法是否返回结果。我的查找方法如下:

post = Post.find(:all, :conditions => { :url => params['url'] }, :limit => 1)

What would be a good way to check that postcontains a result?

检查post包含结果的好方法是什么?

回答by Jordan Running

find :allreturns an empty array ([]) if no rows are returned, so you can just use it this way:

find :all[]如果没有返回行,则返回一个空数组 ( ),因此您可以这样使用它:

post = Post.find(:all, :conditions => { :url => params['url'] }, :limit => 1)

unless post.empty?
  # do something...
end

By the way, if you do find :allyou're going to get an array, not a single row. If you're trying to get just one Post, it would be cleaner to use the find_byhelper or find :firstor just firstinstead:

顺便说一句,如果你这样做,find :all你会得到一个数组,而不是一行。如果您只想获得一个 Post,则使用find_byhelper会更干净,find :first或者只是first代替:

post = Post.find_by_url params['url']

# or

post = Post.first :conditions => { :url => params['url'] }

# then...

if post
  # do something...
end

回答by shingara

You can try ActiveRecord::Base.exists?before

你可以试试ActiveRecord::Base.exists?

Post.exists?(:conditions => { :url => params['url'] })

回答by Ryan Bigg

Use the BANG!version of the find_by_urlmethod to get it to raise an exception of it could not be found and then rescue it later on in that same method/action.

使用砰!find_by_url无法找到使其引发异常的方法版本,然后稍后在同一方法/操作中拯救它。

def show
  Post.find_by_url!(params[:url])
  rescue ActiveRecord::RecordNotFound
    flash[:notice] = "The URL you were looking for could not be found."
    redirect_to root_path
  end
end

If you didn't raise an exception here I believe that Rails would show the public/404.htmlpage.

如果您没有在这里提出异常,我相信 Rails 会显示public/404.html页面。

回答by Cristobal Viedma

if post doesn't contain any result it will be an empty list and then:

如果帖子不包含任何结果,它将是一个空列表,然后:

post.empty?

will return true.

将返回真。

回答by jamin4jc

it may be as simple as changing your finder to:

它可能就像将您的查找器更改为:

post = Post.find(:first, :conditions => { :url => params['url'] })

With this finder, post will either return a single value or nil. Because nil behaves like false in a condition statement, you can say something like the following:

使用此查找器,post 将返回单个值或 nil。因为 nil 在条件语句中表现得像 false,所以你可以这样说:

if post
  # do something
else
  # do something else
end

回答by boulder_ruby

Post.find_by_id(id_column_value)

will return nil rathering than blowing up your program when it can't find a record.

当它找不到记录时,将返回 nil 而不是炸毁你的程序。

Of course, there's

当然,还有

x = Post.where(:any_column_name => value) 

which always returns an array of results. In which case you could just run an

它总是返回一个结果数组。在这种情况下,您可以运行一个

x.each {|t| f(t) }

or

或者

y = x.map {|t| f(t)}

or of course,

或者当然

x[0], x[1], etc

Sorry I got a little carried away there

对不起,我有点忘乎所以

回答by Vlad Cenan

Another way to do it is checking with ActiveRecord#any?.

另一种方法是使用ActiveRecord#any 进行检查.