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?

Anyone?

On Sep 14, 1:28 am, rbibby [email protected] 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?

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.

Matthew R. 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}

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 R. [email protected]