Validation not bubbling up to my other models.

Posted by DJTripleThreat on Stack Overflow See other posts from Stack Overflow or by DJTripleThreat
Published on 2010-03-22T05:42:53Z Indexed on 2010/03/26 10:53 UTC
Read the original article Hit count: 376

Ok, I have a relationship between People, Users and Employees such that All Employees are Users and all Users are People. Person is an abstract class that User is derived from and Employee is derived from that.

Now... I have an EmployeesController class and the create method looks like this:

def create
  @employee = Employee.new(params[:employee])
  @employee.user = User.new(params[:user])
  @employee.user.person = Person.new(params[:person])

  respond_to do |format|
    if @employee.save
      flash[:notice] = 'Employee was successfully created.'
      format.html { redirect_to(@employee) }
      format.xml  { render :xml => @employee, :status => :created, :location => @employee }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @employee.errors, :status => :unprocessable_entity }
    end
  end
end

As you can see, when I'm using the :polymorphic => true clause, the way you access the super class is by doing something like @derived_class_variable.super_class_variable.super_super_etc. The Person class has a validates_presence_of :first_name and when it is satisfied, on my form, everything is OK. However, if I leave out the first name, it won't prevent the employee from being saved. What happens is that the employee record is saved but the person record isn't (because of the validation).

How can I get the validation errors to show up in the flash object?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about polymorphism