# Most efficient/best way to do favorites or a Digg-style page

**URL:** <https://www.ruby-forum.com/t/most-efficient-best-way-to-do-favorites-or-a-digg-style-page/114753>\
**Category:** Rails\
**Created:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/most-efficient-best-way-to-do-favorites-or-a-digg-style-page/114753 "2007-09-17T13:25:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbibby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rbibby](https://www.ruby-forum.com/u/rbibby)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/most-efficient-best-way-to-do-favorites-or-a-digg-style-page/114753/1 "2007-09-17T13:25:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rbibby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rbibby](https://www.ruby-forum.com/u/rbibby)\
**Post date:** [September 18, 2007, 5:46am UTC](https://www.ruby-forum.com/t/most-efficient-best-way-to-do-favorites-or-a-digg-style-page/114753/2 "2007-09-18T05:46:47Z")

</div>

Anyone?

On Sep 14, 1:28 am, rbibby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![rbibby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rbibby](https://www.ruby-forum.com/u/rbibby)\
**Post date:** [September 18, 2007, 11:08am UTC](https://www.ruby-forum.com/t/most-efficient-best-way-to-do-favorites-or-a-digg-style-page/114753/3 "2007-09-18T11:08:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rbibby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rbibby](https://www.ruby-forum.com/u/rbibby)\
**Post date:** [September 18, 2007, 11:09am UTC](https://www.ruby-forum.com/t/most-efficient-best-way-to-do-favorites-or-a-digg-style-page/114753/4 "2007-09-18T11:09:51Z")

</div>

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}

---

<div class="post-metadata">

**Author:** ![rbibby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rbibby](https://www.ruby-forum.com/u/rbibby)\
**Post date:** [September 20, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/most-efficient-best-way-to-do-favorites-or-a-digg-style-page/114753/5 "2007-09-20T16:10:34Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
