# A "User has\_many :files", a "File has\_many :users" - How?

**URL:** <https://www.ruby-forum.com/t/a-user-has-many-files-a-file-has-many-users-how/119085>\
**Category:** Rails\
**Created:** [November 2, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/a-user-has-many-files-a-file-has-many-users-how/119085 "2007-11-02T23:38:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 2, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/a-user-has-many-files-a-file-has-many-users-how/119085/1 "2007-11-02T23:38:06Z")

</div>

User – “has many” :files  
File – “has many” :users

How do you define that relationship?

If a user has many files, and each of those files is shared by numerous  
other users, how do you define the relationship?

I’m absolutely stuck on this. Any ideas?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 3, 2007, 12:53am UTC](https://www.ruby-forum.com/t/a-user-has-many-files-a-file-has-many-users-how/119085/2 "2007-11-03T00:53:17Z")

</div>

I’m also a newbie. Here’s how I do it:

There is a technique called habtm – has and belongs to many.

I am not going to show you habtm, because I don’t use it. Habtm  
describes the trivial situation where the pivot table contains only  
ids from the two tables it joins. You may find you want other data in  
your join table besides just those two ids, and thus you would use the  
method I am describing. It is in the section in Agile Rails  
Development called “Using Models as Join Tables”.

You make what’s called a “pivot table”, or an “intermediate join  
table”. It is a table that sits between your two tables. At the very  
least, it has an id from each table. Your model would be called

user\_file

your table would be called

user\_files

and it would consist of

user\_id  
file\_id

but could have other columns as well.

Then, in your user\_file model, you have these lines:

belongs\_to :user  
belongs\_to :file

(notice the pluralization – when you talk about a model on the “one”  
side of a one-to-many relationship, it’s singular - -on the many side,  
it’s plural)

in your user model, you have these lines

has\_many :user\_files  
has\_many :files, :through =\> :user\_files

and your user table must have, at the very least, these columns:

id  
user\_file\_id

then of course in your file model, you have

has\_many :user\_files  
has\_many :users, :through =\> :user\_files

and your file model must have, at the very least, these columns

id  
user\_file\_id

And rails style says that all these id columns should be integers, but  
if you want to fight that, there are ways.

That’s it, good luck.

Charlie

On Nov 2, 3:38 pm, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 3, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/a-user-has-many-files-a-file-has-many-users-how/119085/3 "2007-11-03T18:04:00Z")

</div>

Another (semi?)newbie chiming in:

Charlie described one method, which is certainly good if you have more  
complex relationships. But if you really just need a simple many to  
many relationship I find join tables and has\_and\_belongs\_to\_many works  
well.

Your user table has an id field.  
Your file table has an id field.  
You then have a join table. The join table has only the id from the  
other two fields. The name of the join table is the plural forms of  
both tables joined with an underscore, in alphabetical order.

If you are using migrations your join table should look something like  
this:

create\_table “files\_users”, :id =\> false, :force =\> true do |t|  
t.column “file\_id”, :integer, :null =\> false  
t.column “user\_id”, :integer, :null =\> false  
end

Notice the :id =\> false. This is so your migration does not create an  
id in the join table.

Now, your models.

user.rb

has\_and\_belongs\_to\_many :files  
#other associations

file.rb

has\_and\_belongs\_to\_many :users

Thats it 🙂

If you do a Google search for “rails habtm” you will get lots of  
resources.

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 3, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/a-user-has-many-files-a-file-has-many-users-how/119085/4 "2007-11-03T19:42:18Z")

</div>

this looks like a many to many question.  
use [http://wiki.rubyonrails.org/rails/pages/has\_and\_belongs\_to\_many](http://wiki.rubyonrails.org/rails/pages/has_and_belongs_to_many)

use in your model:  
has\_and\_belongs\_to\_many

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 5, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/a-user-has-many-files-a-file-has-many-users-how/119085/5 "2007-11-05T21:40:53Z")

</div>

Thanks Charlie! I really appreciate your help 🙂

You’re the best. Thank you.

Gunde, thank you too!
