Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails 4 find with order and limit equivalent

I need very fast to update my app controller to rails 4, but i didn't know how to write all this line in new rails 4 style: with order_by with conditions and with limit, in web i search for fast, and all part's together - didn't find. How could i translate this from rails 3 to rails 4?

in controller i have

@news = Article.find(:all, conditions: { text: "example" }, order_by: "created_at", limit: 4 )

i didn't must to use conditions in 4, only where, but how to append :all, order_by and limit to it? What is a right syntax?

like image 529
Valdis Azamaris Avatar asked Feb 17 '14 16:02

Valdis Azamaris


1 Answers

In Arel (https://github.com/rails/arel) everything can be appended. The various clauses are described here: http://guides.rubyonrails.org/active_record_querying.html

@news = Article.where(text: 'example').order(:created_at).limit(4)

Note that this returns a query clause that does not actually query the database until you specifically request the data.

like image 121
GSP Avatar answered Nov 15 '22 20:11

GSP