Validation not bubbling up to my other models
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 开发者_如何学Python: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?
What I needed was validates_associated
so i would have:
employee.rb
validates_associated :user
user.rb
validates_associated :person
精彩评论