# Many to Many

**URL:** <https://www.ruby-forum.com/t/many-to-many/93922>\
**Category:** Rails\
**Created:** [April 14, 2006, 5:21am UTC](https://www.ruby-forum.com/t/many-to-many/93922 "2006-04-14T05:21:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [April 14, 2006, 5:21am UTC](https://www.ruby-forum.com/t/many-to-many/93922/1 "2006-04-14T05:21:00Z")

</div>

Hi, I have three tables, a domain of Sites, a domain of Users, and a  
junction of Sites\_Users with the tables named as such.

My RoR application has a model and controller both User and Site. I  
tried to add a controller and model named Site\_User but I can’t  
reference any functions in the controller, I get an Uninitialized  
Constant error.

I’m very new to RoR, but not to database development, so I’m not sure  
what I’m doing incorrectly here. Hopefully someone can point me in the  
right direction.

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [April 14, 2006, 5:59am UTC](https://www.ruby-forum.com/t/many-to-many/93922/2 "2006-04-14T05:59:28Z")

</div>

On Fri, Apr 14, 2006 at 05:21:00AM +0200, Joe C. wrote:

> right direction.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I don’t see why you would want a model named Site\_User, or a controller.  
It is not necicarry to have the join table as a model in order for HABTM  
relationship to work, as long as the Sites\_Users table has two fields,  
site\_id and user\_id, you can simply put in the user model:  
has\_and\_belongs\_to\_many :sites  
And in the site model:  
has\_and\_belongs\_to\_many :users

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [April 14, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/many-to-many/93922/3 "2006-04-14T15:02:18Z")

</div>

That may be because in ruby, constants are defined with Uppercase  
characters, wheras variables are defined lowercase. You should look at  
lowercasing some of your variable names that you don’t want explicitly  
as  
constants.

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [April 14, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/many-to-many/93922/4 "2006-04-14T14:56:58Z")

</div>

Christian wrote:

> On Fri, Apr 14, 2006 at 05:21:00AM +0200, Joe C. wrote:
> 
> > right direction.
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> I don’t see why you would want a model named Site\_User, or a controller.  
> It is not necicarry to have the join table as a model in order for HABTM  
> relationship to work, as long as the Sites\_Users table has two fields,  
> site\_id and user\_id, you can simply put in the user model:  
> has\_and\_belongs\_to\_many :sites  
> And in the site model:  
> has\_and\_belongs\_to\_many :users

Ok, I didn’t think I needed the model, but added one when things weren’t  
working. However, I want a function to find if a user is a member of a  
certain site. So I added a controller with that functionality.

This is where I get the Uninitialized Constant problem…  
when I call: Site\_user.ismember?
