OK、私は人、ユーザー、従業員の間に、すべての従業員がユーザーであり、すべてのユーザーが人であるという関係があります。は、そこから派生し、そこから派生するPerson
抽象クラスです。User
Employee
今...私はEmployeesController
クラスを持っていて、create
メソッドは次のようになります:
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
ご覧のとおり、この:polymorphic => true
句を使用している場合、スーパークラスにアクセスする方法は、のような操作を行うことです@derived_class_variable.super_class_variable.super_super_etc
。Person
クラスにはがあり、それが満たされたvalidates_presence_of :first_name
とき、私のフォームでは、すべてがOKです。ただし、名を省略しても、従業員の救済を妨げることはありません。何が起こるかというと、従業員レコードは保存されますが、個人レコードは保存されません(検証のため)。
検証エラーをフラッシュオブジェクトに表示するにはどうすればよいですか?