Ruby Rails 3-同时使用外键保存少数型号
我有用户,教师,教师教育教师教育属于教师,教师属于用户 我使用嵌套属性通过控制器中的一行保存所有内容Ruby Rails 3-同时使用外键保存少数型号,ruby,model,nested-attributes,ruby-on-rails-3.2,Ruby,Model,Nested Attributes,Ruby On Rails 3.2,我有用户,教师,教师教育教师教育属于教师,教师属于用户 我使用嵌套属性通过控制器中的一行保存所有内容user.save。但我遇到了我无法解决的事情。我可以为教师设置id,但在保存教师之前,我不能为教师教育设置id 是否可以修复此问题并在模型中继续使用嵌套属性 class User < ActiveRecord::Base attr_accessor :password
user.save
。但我遇到了我无法解决的事情。我可以为教师设置id,但在保存教师之前,我不能为教师教育设置id
是否可以修复此问题并在模型中继续使用嵌套属性
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :user_login,
:password,
:teacher_attributes
has_one :teacher
accepts_nested_attributes_for :teacher
end
class Teacher < ActiveRecord::Base
attr_accessible :teacher_last_name,
:teacher_first_name,
:teacher_middle_name,
:teacher_birthday,
:teacher_sex,
:teacher_category,
:teacher_education_attributes
belongs_to :user
has_one :teacher_education
accepts_nested_attributes_for :teacher_education
validates :user_id,
:presence => true
end
class TeacherEducation < ActiveRecord::Base
attr_accessible :teacher_education_university,
:teacher_education_year,
:teacher_education_graduation,
:teacher_education_speciality
belongs_to :teacher
validates :teacher_id,
:presence => true
...
end
查看-new\u teacher.html.erb
<%= form_for @user, :url => create_teacher_url, :html => {:class => "form-horizontal"} do |f| %>
<%= field_set_tag do %>
<%= f.fields_for :teacher do |builder| %>
<div class="control-group">
<%= builder.label :teacher_last_name, "Фамилия", :class => "control-label" %>
<div class="controls">
<%= builder.text_field :teacher_last_name, :value => @teacher_last_name %>
</div>
</div>
<div class="control-group">
<%= builder.label :teacher_first_name, "Имя", :class => "control-label" %>
<div class="controls">
<%= builder.text_field :teacher_first_name, :value => @teacher_first_name %>
</div>
</div>
<div class="control-group">
<%= builder.label :teacher_middle_name, "Отчество", :class => "control-label" %>
<div class="controls">
<%= builder.text_field :teacher_middle_name, :value => @teacher_middle_name %>
</div>
</div>
<div class="control-group">
<%= builder.label :teacher_sex, "Пол", :class => "control-label" %>
<div class="controls">
<%= label_tag nil, nil, :class => "radio" do %>
<%= builder.radio_button :teacher_sex, 'm', :checked => @user_sex_man %>
Мужской
<% end %>
<%= label_tag nil, nil, :class => "radio" do %>
<%= builder.radio_button :teacher_sex, 'w', :checked => @user_sex_woman %>
Женский
<% end %>
</div>
</div>
<div class="control-group">
<%= builder.label :teacher_birthday, "Дата рождения", :class => "control-label" %>
<div class="controls">
<%= builder.text_field :teacher_birthday, :value => @teacher_birthday %>
<p class="help-block">Формат даты: дд.мм.гггг</p>
</div>
</div>
<div class="control-group">
<%= builder.label :teacher_category, "Категория", :class => "control-label" %>
<div class="controls">
<%= builder.text_field :teacher_category, :value => @teacher_category %>
</div>
</div>
<%= builder.fields_for :teacher_education do |edu_fields| %>
<div class="control-group">
<%= edu_fields.label :teacher_education_university, "Название ВУЗа", :class => "control-label" %>
<div class="controls">
<%= edu_fields.text_field :teacher_education_university, :value => @teacher_university %>
</div>
</div>
<div class="control-group">
<%= edu_fields.label :teacher_education_year, "Дата выпуска из ВУЗа", :class => "control-label" %>
<div class="controls">
<%= edu_fields.text_field :teacher_education_year, :value => @teacher_finish_univ %>
<p class="help-block">Формат даты: дд.мм.гггг</p>
</div>
</div>
<div class="control-group">
<%= edu_fields.label :teacher_education_graduation, "Степень", :class => "control-label" %>
<div class="controls">
<%= edu_fields.text_field :teacher_education_graduation, :value => @teacher_graduation %>
</div>
</div>
<div class="control-group">
<%= edu_fields.label :teacher_education_speciality, "Специальность", :class => "control-label" %>
<div class="controls">
<%= edu_fields.text_field :teacher_education_speciality, :value => @teacher_specl %>
</div>
</div>
<% end %>
<% end %>
<hr/>
<div class="control-group">
<%= f.label :user_login, "Логин учетной записи", :class => "control-label" %>
<div class="controls">
<%= f.text_field :user_login, :value => @user_login %>
<%= link_to_function "Сгенерировать логин", "generate_login()", :class => "btn" %>
</div>
</div>
<div class="control-group">
<%= f.label :password, "Пароль учетной записи", :class => "control-label" %>
<div class="controls">
<%= f.text_field :password, :value => @user_password %>
<%= link_to_function "Сгенерировать пароль", "generate_password()", :class => "btn" %>
</div>
</div>
<% end %>
<%= f.submit "Создать", :class => "btn btn-large btn-success" %>
<% end %>
我的模式
create_table "teacher_educations", :force => true do |t|
t.integer "teacher_id"
t.string "teacher_education_university"
t.date "teacher_education_year"
t.string "teacher_education_graduation"
t.string "teacher_education_speciality"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "teacher_phones", :force => true do |t|
t.integer "teacher_id"
t.string "teacher_home_number"
t.string "teacher_mobile_number"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "teachers", :force => true do |t|
t.integer "user_id"
t.string "teacher_last_name"
t.string "teacher_first_name"
t.string "teacher_middle_name"
t.date "teacher_birthday"
t.string "teacher_sex"
t.string "teacher_category"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "user_login"
t.string "user_role"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "encrypted_password"
t.string "salt"
end
如果要正确构造表单以支持嵌套属性,则只需执行以下操作:
user = User.new( params[:user] )
user.user_role = "teacher"
if user.save
...
end
接受嵌套属性机制将处理其余部分。如果上述操作不起作用,那么让我们看看表单是如何组合的。如果您正确构建表单以支持嵌套属性,那么这就是您所需要的:
user = User.new( params[:user] )
user.user_role = "teacher"
if user.save
...
end
接受嵌套属性机制将处理其余部分。如果上述操作不起作用,那么让我们看看表单是如何组合的。您是否更新了控制器操作,使其看起来像我的答案?此外,“不能为空”验证来自哪里?上面列出的模型代码不包括任何。不要将状态检查应用于这样的关联。将状态检查应用于has_one关联。我更新了我的模型(教师和教师教育),以显示生成此类错误的验证。此外,我还有很多名称和其他字段的验证(但我不认为这很有趣)…我按照你的建议从模型中删除了检查id的过程,现在它就像一个符咒一样工作!非常感谢。您是否将控制器操作更新为与我的答案相同?此外,“不能为空”验证来自哪里?上面列出的模型代码不包括任何。不要将状态检查应用于这样的关联。将状态检查应用于has_one关联。我更新了我的模型(教师和教师教育),以显示生成此类错误的验证。此外,我还有很多名称和其他字段的验证(但我不认为这很有趣)…我按照你的建议从模型中删除了检查id的过程,现在它就像一个符咒一样工作!非常感谢。
create_table "teacher_educations", :force => true do |t|
t.integer "teacher_id"
t.string "teacher_education_university"
t.date "teacher_education_year"
t.string "teacher_education_graduation"
t.string "teacher_education_speciality"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "teacher_phones", :force => true do |t|
t.integer "teacher_id"
t.string "teacher_home_number"
t.string "teacher_mobile_number"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "teachers", :force => true do |t|
t.integer "user_id"
t.string "teacher_last_name"
t.string "teacher_first_name"
t.string "teacher_middle_name"
t.date "teacher_birthday"
t.string "teacher_sex"
t.string "teacher_category"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "user_login"
t.string "user_role"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "encrypted_password"
t.string "salt"
end
user = User.new( params[:user] )
user.user_role = "teacher"
if user.save
...
end