Rick Martinez
2006-Jul-26 16:25 UTC
[Rails] 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
