I'm using Rails 3 and have a rich association between Projects and Users. The join model (UsersProject
) has an additional boolean attribute, administrator
, which flags the User
as an admin of Project
:
Sure I'm missing something obvious here, but is there a clean way to set the administrator
attribute on the join model (UsersProject
) when creating a new Project? e.g:
class Project < ActiveRecord::Base
has_many :users_projects
has_many :users, :through => :users_projects
end
class User < ActiveRecord::Base
has_many :users_projects
has_many :projects, :through => :users_projects
# Guessing I use something along these lines, although should I be using scope?
# has_many :administered_projects,
# :through => :users_projects,
# :source => :project,
# :conditions => ['users_projects.administrator = ?', true]
# ...
end
class UsersProject < ActiveRecord::Base
# Join model has an boolean attribute :administrator
belongs_to :user
belongs_to :project
end
# INTENDED USAGE:
@project = @user.administered_projects.new(params[:project])
# => Creates a UsersProject record with `:administrator=true`
@project = @user.projects.new(params[:project])
# => Creates a UsersProject record with `:administrator=false`
Appreciate any help, Chris