egervari
2011-May-13 05:09 UTC
Just wanted to say rails 3 active relation can be rather great!
Here''s a query I just built as I build my application and learn rails:
def self.views
sorted
.select("help_categories.id, help_categories.name,
COUNT(help_documents.id) AS number_of_documents")
.joins("left outer join help_documents on help_categories.id
help_documents.category_id")
.group(:id)
.group(:name)
end
I just want to say that this query works and is so much more straight-
forward compared to using Hibernate. In the Hibernate world, you first
have to make a static class to store id, name and the
number_of_documents... and the query and boiler plate is immense:
public List<HelpCategoryView> findAllViews() {
return getHibernateTemplate().executeFind(new
HibernateCallback<List<HelpCategoryView>>() {
@Override
public List<HelpCategoryView> doInHibernate(Session
session) throws HibernateException, SQLException {
Query query = session.createQuery(
"select new
trainingdividend.domain.sysadmin.HelpCategoryView(" +
" helpCategory.id, helpCategory.name,
count(helpDocument)) " +
"from HelpCategory helpCategory " +
" left join helpCategory.documents helpDocument
" +
"group by helpCategory.id, helpCategory.name " +
"order by helpCategory.name asc"
);
return query.list();
}
});
}
So congrats!
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.