7

我正在使用RubyMinewithrails 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.
4

3 回答 3

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!

您可以从此位置阅读更多信息

不推荐使用的语句

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

于 2013-02-26T20:52:34.840 回答
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
于 2014-03-24T20:20:00.437 回答
3

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

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

#User.find(:first)
User.first

#User.find(:all, :conditions => {:foo => true})
User.where(:foo => true).all
于 2013-02-26T21:11:46.920 回答