Using validators for appending to a HABTM association

class User < ActiveRecord::Base
has_and_belongs_to_many :projects
end

class Project < ActiveRecord::Base
has_and_belongs_to_many :users
end

class ProjectController < ApplicationController
def add_user
@project = Project.find(params[:project_id])
@user = User.find(params[:id])
@project.users << @user unless @project.users.include?(@user)
# I’d honestly rather do the above with a validator instead of a
condition
# This way I can show the error to the person using the error
validators I have now
# What would be the best way of doing this?
# Thanks!
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs