I can’t get how to do one thing. I have models User, Teacher, TeacherEducation. TeacherEducation belongs to Teacher, Teacher belongs to User. I would like to save User, Teacher and TeacherEducation in one line by user.save in my controller. Is that possible? If no then which is better way to save that?
class User < ActiveRecord::Base
attr_accessible ...,
:teacher_attributes
has_one :teacher
accepts_nested_attributes_for :teacher
end
class Teacher < ActiveRecord::Base
belongs_to :user
has_one :teacher_education
accepts_nested_attributes_for :teacher_education
end
class TeacherEducation < ActiveRecord::Base
belongs_to :teacher
end
Right now without TeacherEducation i have such view
<%= form_for @user, :url => create_teacher_url, :html => {:class => "form-horizontal"} do |f| %>
<%= field_set_tag do %>
<%= f.fields_for :teacher do |builder| %>
# Fields of teacher
<% end %>
# Fields of user
<% end %>
My controller
class AdminsController < ApplicationController
def new_teacher
@user = User.new
teacher = @user.build_teacher
...
end
def create_teacher
user = User.new( params[:user] )
if user.save
...
end
end
end
This should work, I think..