rbibby
2007-Sep-14 05:28 UTC
Most efficient/best way to do favorites or a Digg-style page?
Hi, Suppose I''m building a site that lets users mark items as favorites, or as an example, a Digg-style site, where users can vote up or down items... What is the most efficient way to give each item the appropriate context based on whether it has been marked/voted the next time it appears on a list of items for the user? For example, if you Digg something on Digg, it remembers the next time you see that item on a dynamically generated list (whether it''s the most popular, front page, etc.) and shows that it has already been dugg and gives you a different set of options for that item. I see 2 possible solutions, neither of which seems particularly efficient: 1. When loading the page, do a database join of user''s diggs with news items to get the status of each item 2. Load all of a user''s diggs into memory as an array, and in the view, for each item do something like... if item.id in user_votes then ... Which of these is better, or is there even a better way? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
rbibby
2007-Sep-18 03:45 UTC
Re: Most efficient/best way to do favorites or a Digg-style page?
Anyone? On Sep 14, 1:28 am, rbibby <ricky...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Suppose I''m building a site that lets users mark items as favorites, > or as an example, a Digg-style site, where users can vote up or down > items... What is the most efficient way to give each item the > appropriate context based on whether it has been marked/voted the next > time it appears on a list of items for the user?> For example, if you > Digg something on Digg, it remembers the next time you see that item > on a dynamically generated list (whether it''s the most popular, front > page, etc.) and shows that it has already been dugg and gives you a > different set of options for that item. > > I see 2 possible solutions, neither of which seems particularly > efficient: > 1. When loading the page, do a join of user''s diggs with news > items to get the status of each item > 2. Load all of a user''s diggs into memory as an array, and in the > view, for each item do something like... if item.id in user_votes > then <customized options> > > Which of these is better, or is a way that''s better yet?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Sep-18 09:08 UTC
Re: Most efficient/best way to do favorites or a Digg-style
rbibby wrote:> Hi, > > Suppose I''m building a site that lets users mark items as favorites, > or as an example, a Digg-style site, where users can vote up or down > items... What is the most efficient way to give each item the > appropriate context based on whether it has been marked/voted the next > time it appears on a list of items for the user? For example, if you > Digg something on Digg, it remembers the next time you see that item > on a dynamically generated list (whether it''s the most popular, front > page, etc.) and shows that it has already been dugg and gives you a > different set of options for that item. > > I see 2 possible solutions, neither of which seems particularly > efficient: > 1. When loading the page, do a database join of user''s diggs with news > items to get the status of each item > 2. Load all of a user''s diggs into memory as an array, and in the > view, for each item do something like... if item.id in user_votes > then ... > > Which of these is better, or is there even a better way?how about this; grab all the content you''re going to display, then grab all the user_diggs for those content_ids ie. @content = Content.find_most_popular(:limit => 20) user_diggs = UserDiggs.find(:all, :select => "content_id", :conditions => ["content_id IN(?)", @content.map(&:id)]) @dugg_ids = user_diggs.inject({}){|hash, id| hash[id] = true} then in the view, make a helper like; def has_dugg?(content) @dugg_ids[content.id] end something like that. -- 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 -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Sep-18 09:09 UTC
Re: Most efficient/best way to do favorites or a Digg-style
Matthew Rudy wrote:> ie. > @content = Content.find_most_popular(:limit => 20) > user_diggs = UserDiggs.find(:all, :select => "content_id", :conditions > => ["content_id IN(?)", @content.map(&:id)]) > > @dugg_ids = user_diggs.inject({}){|hash, id| hash[id] = true}correction: @dugg_ids = user_diggs.inject({}){|hash, digg| hash[digg.content_id] = true} -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the response. This seems like the most scalable, efficient way to do it, which is exactly what I was looking for. Thanks! On Sep 18, 5:09 am, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Matthew Rudy wrote: > > ie. > > @content = Content.find_most_popular(:limit => 20) > > user_diggs = UserDiggs.find(:all, :select => "content_id", :conditions > > => ["content_id IN(?)", @content.map(&:id)]) > > > @dugg_ids = user_diggs.inject({}){|hash, id| hash[id] = true} > > correction: > @dugg_ids = user_diggs.inject({}){|hash, digg| hash[digg.content_id] > true} > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---