All pastes #956470 Raw Edit

yango

public ruby v1 · immutable
#956470 ·published 2008-03-25 10:09 UTC
rendered paste body
#Here goes the description of what happens, below is the related code#I edit User (with habtm on Program) and validates_presence_of :programs but, even when the validation is triggered, the join table#gets inconsistent:-> Original table:mysql> select user_id,program_id,logname,p.name from programs_users pu,users u, programs p where user_id = u.id and program_id = p.id;+---------+------------+----------+------+| user_id | program_id | logname  | name |+---------+------------+----------+------+|       2 |          2 | iherrera | SIT  |+---------+------------+----------+------+1 row in set (0.00 sec)-> Create a new usermysql> select user_id,program_id,logname,p.name from programs_users pu,users u, programs p where user_id = u.id and program_id = p.id;+---------+------------+----------+------+| user_id | program_id | logname  | name |+---------+------------+----------+------+|       2 |          2 | iherrera | SIT  ||       3 |          1 | Bar      | SAT  |+---------+------------+----------+------+-> Edit the user and remove all programs, I get validation error but the table gets hit anyway!!1 error prohibited this user from being savedThere were problems with the following fields:    * Programs cant be blankbutmysql> select user_id,program_id,logname,p.name from programs_users pu,users u, programs p where user_id = u.id and program_id = p.id;+---------+------------+----------+------+| user_id | program_id | logname  | name |+---------+------------+----------+------+|       2 |          2 | iherrera | SIT  |+---------+------------+----------+------+1 row in set (0.01 sec)#Models related in habtm#$ more app/models/user.rbclass User < ActiveRecord::Base  has_and_belongs_to_many :programs  validates_presence_of :programsend#$ more app/models/program.rbclass Program < ActiveRecord::Base        belongs_to :project  has_and_belongs_to_many :users  validates_presence_of :name, :project_id  validates_presence_of :project, :message => "does not exist"end#Controller  # POST /users  # POST /users.xml  def create    prog_ids = params[:user][:programs]    params[:user].delete(:programs)    @user = User.new(params[:user])    @user.programs = []    if prog_ids != nil then      prog_ids.each do |pi|        @user.programs << Program.find(pi)      end    end    respond_to do |format|      if @user.save        flash[:notice] = 'User was successfully created.'        format.html { redirect_to(@user) }        format.xml  { render :xml => @user, :status => :created, :location => @user }      else        format.html { render :action => "new" }        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }      end    end  end  # PUT /users/1  # PUT /users/1.xml  def update    @user = User.find(params[:id])    prog_ids = params[:user][:programs]    params[:user].delete(:programs)    @user.programs = []    if prog_ids != nil then      prog_ids.each do |pi|        @user.programs << Program.find(pi)      end    end    respond_to do |format|      if @user.update_attributes(params[:user])        flash[:notice] = 'User was successfully updated.'        format.html { redirect_to(@user) }        format.xml  { head :ok }      else        format.html { render :action => "edit" }        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }      end    end  end