Igor Milovanović
2006-Sep-13 14:21 UTC
add a column count(id) to the select statement, rail way
I would like to do something like this: SELECT *,count(ID) FROM downloads GROUP BY file_path ORDER BY user_id LIMIT 10; I know it can be done with find_by_sql, but don't know if that can be done by just find. Or is there better Rails way to do this? Thanks. -- Igor MILOVANOVIÆ http://f13o.pletisan.rs.ba/blog/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-13 15:26 UTC
Re: add a column count(id) to the select statement, rail way
I think you risk running 2 queries if you do it the rails way of Model.count(). Up to you really. Its probably better to do a custom find_by_sql to limit the queries you run against your DB. Adam On 9/13/06, Igor Milovanović <pletisan.list@gmail.com> wrote:> > I would like to do something like this: > SELECT *,count(ID) FROM downloads GROUP BY file_path ORDER BY user_id > LIMIT 10; > > I know it can be done with find_by_sql, but don't know if that can be > done by just find. > > Or is there better Rails way to do this? > > Thanks. > > -- > Igor MILOVANOVIÆ > http://f13o.pletisan.rs.ba/blog/ > > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thomas, Mark - BLS CTR
2006-Sep-13 20:39 UTC
Re: add a column count(id) to the select statement, rail way
> I would like to do something like this: > SELECT *,count(ID) FROM downloads GROUP BY file_path ORDER BY > user_id LIMIT 10; > > I know it can be done with find_by_sql, but don''t know if > that can be done by just find. > > Or is there better Rails way to do this?If by "Rails way" you mean something built in to ActiveRecord, then you''re talking about counter_cache. class Download < ActiveRecord::Base belongs_to :product, :counter_cache => true end Then in your product table, you would have a downloads_count column (int, default of 0). When you do that you''ll be able to access product.downloads.size *WARNING* I recall there being a prior thread talking about a bug in save() that blows away the count, or something like that. So I don''t know if this solution is broken or not at this point. - Mark. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Igor Milovanović
2006-Sep-14 10:24 UTC
Re: add a column count(id) to the select statement, rail way
Thanks for replies, I will have a look. But this find_by_sql solution is working like a charm for me... So, I might just leave it that way... On 9/13/06, Thomas, Mark - BLS CTR <Thomas.Mark@bls.gov> wrote: -- Igor MILOVANOVIÆ http://f13o.pletisan.rs.ba/blog/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---