Need help on special HABTM relation

hey all,
I have three tables like this:
forum (id,title)
usergroup(id,title)
forum_perms(usergroup_id,forum_id,read,write,post)

is there a way to deal with that kind of relation with rails such as by
using has_and_belongs_to_many kind of stuff?

thanx in advance

Pat

Uhm … Dont see the HABTM relationship here ?!?

From what you describe (which is not much, i have to guess your
relations), would think this is
what you think of:

class ForumPerms ActiveRecord::Base
belongs_to :forum
belongs_to :usergroup
end

class Forum ActiveRecord::Base
has_many :forum_perms
end

class Usergroup ActiveRecord::Base
has_many :forum_perms

def has_access_on(forum,accparams)
forumperm = ForumPerm.new(accparams)
forumperm.forum = forum
forumperm.user = self
forumperm.save
end
end

see? no HABTM, just 2 has_many <-> belongs_to
and i added a method to the user class to easily add a new permission
for a forum to a group:
the you can simply do:

usergroup = Usergroup(1) # select some group for this example
forum = Forum(1) # select some forum for this example
@usergroup.has_access_on(forum, {:read => true, :write => false, :post
=> true } )

what is done in the method can be done “by hand” in the controller too,
its just nicer this way … and maybe there are may be nicer ways,

As always: im more a new rails user, and make many mistakes so dont be
disappointed if i made some errors…

I’m a little fuzzy on what you need your relationships to do as well. Do
you need to establish that a forum has many usergroups and vice versa
through the data in forum_perms?

In that case, it might benefit you to say:

Class Forum ActiveRecord::Base
has_many :usergroups, :through => :forum_perms
end

Class Usergroup ActivRecord::Base
has_many :forums, :through => :forum_perms
end

This would allow :forum_perms to be its own model object, permitting it
to store the id’s for each usergroup/forum relationship, as well other
columns that contain information pertaining to specific permissions.
(HABTM uses a join table which restricts the relationship data to just
two sets of foreign keys).

For more info on using has_many :through, check out:

http://blog.hasmanythrough.com/articles/2006/02/28/association-goodness

I could be way off base on whether or not this is what you’re looking
for, but it’s a thought.

-Bryan

Patrick A. wrote:

hey all,
I have three tables like this:
forum (id,title)
usergroup(id,title)
forum_perms(usergroup_id,forum_id,read,write,post)

is there a way to deal with that kind of relation with rails such as by
using has_and_belongs_to_many kind of stuff?

thanx in advance

Pat

On 10/25/06, Patrick A. [email protected] wrote:

hey all,
I have three tables like this:
forum (id,title)
usergroup(id,title)
forum_perms(usergroup_id,forum_id,read,write,post)

is there a way to deal with that kind of relation with rails such as by
using has_and_belongs_to_many kind of stuff?

Do you have the following relationships between your tables?
a) Does forum have many usergroups?
b) Does usergroup have many forums?

The forum_perms looks like the join table with additional attributes
associated to break the many-to-many relationship between the other two
tables. If so then you need to use has_many :through to deal with this
scenario.

On 10/25/06, Bala P. [email protected] wrote:

is there a way to deal with that kind of relation with rails such as by using has_and_belongs_to_many kind of stuff?

Do you have the following relationships between your tables?
a) Does forum have many usergroups?
b) Does usergroup have many forums?

The forum_perms looks like the join table with additional attributes associated to break the many-to-many relationship between the other two tables. If so then you need to use has_many :through to deal with this scenario.

ok thanx to all. Actually usergroups have many permissions on each
forum.
So I guess what I should do is:

Usergroup has_many :forums, :through => :forum_perms
and
Forum has_many :usergroups, :through => :forum_perms

is that right?

thanx in advance

Pat