Many to Many

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.

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

right direction.


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
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

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.

Christian wrote:

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

right direction.


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
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?