I have a has_many through association with an attribute and some validations on the "join model". When I try to do something like @user.projects << @project
and the association has already been created (thus the uniqueness validation fails), an exception is raised instead of the error being added to the validation errors.
class User
has_many :project_users
has_many :projects, :through => :project_users
class Project
has_many :project_users
has_many :users, :through => :project_users
class ProjectUser
belongs_to :user
belongs_to :project
# ...
if @user.projects << @project
redirect_to 'somewhere'
else
render :new
end
How can I create the association like I would with the <<
method, but calling save
instead of save!
so that I can show the validation errors on my form instead of using a rescue
to catch this and handle it appropriately?