1

I am updating the active record queries of a rails project. I have not been able to find out how to update when the query is
foo.find(:all, :stuff, :id, :thing, @bar)

I would know how to handle a regular.find(:all) or .find(:all, :conditions => {} (find(:first) and find(:all) are deprecated) but have not found anything on this

Community
  • 1
  • 1
Btuman
  • 881
  • 2
  • 9
  • 37

2 Answers2

2

If, for example, your model is User, to find all users you would use:

User.all

and for specific conditions, you would use:

User.where(neck: "long")
Rebitzele
  • 3,252
  • 1
  • 20
  • 21
2

You can use where:

Foo.where(:stuff => stuff, :thing => thing)
#  or
Foo.where("stuff = ? AND thing = ?", stuff, thing)

Also see Rails .where vs .find...

To find all (again as a ActiveRecord::Relation object, rather than an array) you can use scoped:

Foo.scoped
Community
  • 1
  • 1
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • What function does the string serve? – Btuman May 14 '13 at 18:01
  • @Btuman it's to fight SQL injection (it's recommended in the [docs](http://guides.rubyonrails.org/active_record_querying.html#conditions)). – Andy Hayden May 14 '13 at 18:03
  • I thought .where() is to convert the :conditional => {} to the current format exclusively, am I missing something? – Btuman May 14 '13 at 18:06
  • 1
    @Btuman the scoped thing is usually preferred, since it only hits the database when needed, e.g. Foo.scoped.first only *retrieves* the first element from the db, unlike Foo.all.first which retrieves all Foos. – Andy Hayden May 14 '13 at 18:37