find(:first)和find(:all)已弃用

时间:2013-02-26 20:48:38

标签: ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我正在RubyMine使用rails 3.2.12,我在IDE中收到了已弃用的警告。任何想法如何解决这个已弃用的警告?

find(:first) and find(:all) are deprecated in favour of first and all methods. Support will be removed from rails 3.2.

3 个答案:

答案 0 :(得分:13)

我在@keithepley评论

之后改变了我的答案
#Post.find(:all, :conditions => { :approved => true })
Post.where(:approved => true).all

#Post.find(:first, :conditions => { :approved => true })
Post.where(:approved => true).first
or
post = Post.first  or post = Post.first!
or
post = Post.last   or post = Post.last!

<击>

<击>

您可以从this locations

了解更多信息

已弃用声明

Post.find(:all, :conditions => { :approved => true })

更好的版本

Post.all(:conditions => { :approved => true })

最佳版本(1)

named_scope :approved, :conditions => { :approved => true }
Post.approved.all

最佳版本(2)

Post.scoped(:conditions => { :approved => true }).all

<击>

答案 1 :(得分:4)

以下是Rails 3-4的方法:

Post.where(approved: true) # All accepted posts
Post.find_by_approved(true) # The first accepted post
# Or Post.find_by(approved: true)
# Or Post.where(approved: true).first
Post.first
Post.last
Post.all

答案 2 :(得分:3)

使用Rails 3中添加的新ActiveRecord::Relation内容。点击此处查看更多信息:http://guides.rubyonrails.org/active_record_querying.html

而不是#find,在模型上使用#first#last#all等,以及返回ActiveRecord :: Relation的方法,例如{{ 1}}。

#where