Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails query syntax

I'm kinda new on the Rails boat, I would like to know the difference between two types of syntax for queries

The first one I tried is:

User.limit(8).order('created_at DESC').group('created_at').count

The second, which seems to be far more efficient and powerful:

User.count(:order =>'DATE(created_at) DESC', :group =>["DATE(created_at)"], :limit => 8) 

But I don't really understand the use case for both. I'm sure this is something obvious anyway... Thanks!

like image 418
Dimillian Avatar asked May 14 '26 22:05

Dimillian


1 Answers

The first one is rails 3 syntax. And each method used there, i.e, limit, order, group are ActiveRecord:: Relation method. There are various advantages in using the 1st method. ActiveRecord::Relation is one of the core features of rails 3 apart from asset pipeline etc.

Please read this,

http://asciicasts.com/episodes/239-activerecord-relation-walkthrough

like image 99
Manjunath Manoharan Avatar answered May 17 '26 12:05

Manjunath Manoharan