Wednesday, January 13, 2010

'Include' in rails query

For getting a datum from 2 tables, I have used include as follows,

Admin:

belongs_to :user

User:

has_many :admins

There was a condition that i have to sort names of admins, where the names of admins will be in user table.

Hence,

Admin.find(:all, :include=>"user", :order => "users.name")


I felt this easy than sqls, & interesting

1 comment:

Gogolinek said...

Hmmm it looks really nice. I'm just starting with RoR. I can share some opinion later.

Regards, PaweĊ‚ Wojciechowski
Outsourcing IT - Konopnickiej.Com