Rails merging fields in two way has_many through relationship

116 Views Asked by At

I have the following models:

class User < ActiveRecord::Base
  has_many :forum_users
  has_many :forums through: :forum_users
end

class Forum < ActiveRecord::Base
  has_many :forum_users
  has_many :users through: :forum_users
end

class ForumUser < ActiveRecord::Base
  belongs_to :forums
  belongs_to :users
end

ForumUser has an additional "permissions" field to designate what the user can and cannot do within a particular forum.

The problem is that the "permissions" field on ForumUser is much more difficult to access than fields on Rails models normally are. In an ideal world, I would be able to make requests like:

user = forum.users.first
user.permissions

But once it's assigned, the User doesn't even remember the Forum it was attained through in the first place. It seems like there has to be a simple and straightforward Rails way of doing this. If so, what is it?

0

There are 0 best solutions below