# Habtm and also has\_many of the same things

**URL:** <https://www.ruby-forum.com/t/habtm-and-also-has-many-of-the-same-things/103362>\
**Category:** Rails\
**Created:** [May 4, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/habtm-and-also-has-many-of-the-same-things/103362 "2007-05-04T19:15:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [May 4, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/habtm-and-also-has-many-of-the-same-things/103362/1 "2007-05-04T19:15:04Z")

</div>

Hi,  
our app is a situation where users can upload files and also mark files  
uploaded by other users as their favorites. So for the favorites we are  
using a simple many-to-many with a tie table.

So in attempting that it looks like

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :media\_file  
has\_many :media\_file  
end

class MediaFile \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :user  
belongs\_to :user  
end

but that won’t work because obv. if you call

current\_user.media\_file.find(:all)

there is no way to specify which association you are looking for. Or is  
there?

if not what would be the most Rails-ish way of setting this up?

thanks  
Sam

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [May 4, 2007, 7:52pm UTC](https://www.ruby-forum.com/t/habtm-and-also-has-many-of-the-same-things/103362/2 "2007-05-04T19:52:33Z")

</div>

has\_and\_belongs\_to\_many :favorite\_media\_files, :class =\> “MediaFile”  
has\_many :media\_files
