Forum: Ruby on Rails Many to Many

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-04-14 05:21
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.
1d8edfb3fcce3579c878ca8b06f77285?d=identicon&s=25 Christian (Guest)
on 2006-04-14 05:59
(Received via mailing list)
On Fri, Apr 14, 2006 at 05:21:00AM +0200, Joe Cairns wrote:
> right direction.
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> 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
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-04-14 14:56
Christian wrote:
> On Fri, Apr 14, 2006 at 05:21:00AM +0200, Joe Cairns wrote:
>> right direction.
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> 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?
380bc6f9301b510dde6516120d815464?d=identicon&s=25 Benjamin Gorlick (Guest)
on 2006-04-14 15:02
(Received via mailing list)
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.
This topic is locked and can not be replied to.