I have two models: User
and Company
. A company can have many users and a user can have many companies. As you might suggest, this is the perfect place to use a join table. I'm actually using a full blown model to join User
and Company
so that I can specify the role that each user has. The table, companies_users
, therefore has the following columns: user_id
, company_id
and company_role
.
The situation I'm trying to negotiate is one in which I'm creating both a Company
and a User
and would like to specify the company_role
while doing so.
My new
method is as follows:
def new
@user=User.new
@company=@user.companies.build
end
This creates an entry in the companies_users
join table but (obviously) does so in leaving the company_role
blank.
How might I add this bit of info?
Thanks in advance!