Hey guys, I''m sure this is very simple but i just can''t figure it out. I have profiles and each time a user visits a profile, the visit is recorded in the following model... **profile_views** id user_id #the id of the user who is visiting the profile profile_user_id #the id of the user who is being visited viewed_at This is what I am trying to do (in sudo code) Find all the visitors for a particular profile only returning each visitor once and order the whole list by viewed_at This would be an example list.. Mike, 1 min ago Scott, 2 mins ago Andy, 3 mins ago etc etc This is what I have do far... @latest_visitors = ProfileView.find(:all, :conditions => ["profile_user_id = ?", params[:id]], :order => ''viewed_at DESC'', :limit => 5, :group => ''user_id'') This returns something along the line of what I want - it returns only 1 record for each user, but it returns the very first time that user visited the profile. Any ideas what I''m doing wrong? Thank you!! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Try this: @latest_visitors = ProfileView.find(:all, :conditions => ["profile_user_id = ?", params[:id]], :select => ''id, user_id, profile_user_id, max(viewed_at) viewed_at'', :limit => 5, :group => ''user_id'') You''ll have to include any other fields you need in the "select" option. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Scott Holland
2007-Jan-10 19:47 UTC
Re: Grouping. Difficult find statement. Brain hurting.
Snowman wrote:> Try this: > > @latest_visitors = ProfileView.find(:all, > :conditions => ["profile_user_id = ?", params[:id]], > :select => ''id, user_id, profile_user_id, max(viewed_at) viewed_at'', > :limit => 5, :group => ''user_id'') > > You''ll have to include any other fields you need in the "select" option.That worked a treat! thank you!! I added an '':order'' clause to it and it does everything i want. Was it ok to add the '':order'' or is this bad form? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Scott Holland
2007-Jan-10 19:48 UTC
Re: Grouping. Difficult find statement. Brain hurting.
@latest_visitors = ProfileView.find(:all, :conditions => ["profile_user_id = ?", params[:id]], :select => ''id, user_id, profile_user_id, max(viewed_at) viewed_at'', :limit => 5, :group => ''user_id'', :order => "viewed_at DESC") oops. Meant to add your updated code -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Was it ok to add the '':order'' or is this bad form?Can''t see any reason not to, if it does what you need. I should have left it in but I didn''t pay close enough attention to the description of what you were trying to achieve. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---