7

RubyMinewithを使用し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.
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

Use the new ActiveRecord::Relation stuff that was added in Rails 3. Find more info here: http://guides.rubyonrails.org/active_record_querying.html

Instead of #find, use #first, #last, #all, etc. on your model, and the methods that return a ActiveRecord::Relation, like #where.

#User.find(:first)
User.first

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