7

I am using RubyMine with rails 3.2.12 and I am getting following deprecated warning in my IDE. Any Idea How can I solve this deprecated warning?

find(:first) and find(:all) are deprecated in favour of first and all methods. Support will be removed from rails 3.2.
Sanjay Patel
  • 73
  • 1
  • 1
  • 3

3 Answers3

13

I changed my answer after @keithepley comment

#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!

You can read more from this locations

deprecated statement

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

better version

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

best version (1)

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

best version (2)

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

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
  • 3
    The question's about Rails 3, but this is all outdated Rails 2 syntax. named_scope, scoped, and all with the hash like that are all old. – keithepley Feb 26 '13 at 21:28
  • @keithepley - you are right, I checked in http://guides.rubyonrails.org/active_record_querying.html, thanks – AMIC MING Feb 27 '13 at 02:01
4

Here is the Rails 3-4 way of doing it:

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
Dorian
  • 22,759
  • 8
  • 120
  • 116
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
keithepley
  • 4,760
  • 3
  • 23
  • 41
  • Is there a difference between `User.where(:foo => true).all` and `User.all(:conditions => {:foo => true})` ? – Luis Ortega Araneda May 27 '14 at 19:53
  • 1
    As for the return value, no (in Rails 2 and 3). Rails 4 will return an ActiveRecord::Relation instead. The conditions hash is deprecated though – keithepley May 28 '14 at 14:43