2012-03-21 10 views
1

同じ時間に外部キーを持ついくつかのモデルを保存します。 教師教育は、に属します。教師は、ユーザーに属します。Railsの3 - 私はモデル<strong>ユーザー</strong>、<strong>教師</strong>、<strong>TeacherEducation</strong>を持って

ネストされた属性を使用して、コントローラ内のすべての行を1行で保存します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 

私のコントローラ

class AdminsController < ApplicationController 
def create_teacher 
    user = User.new(params[:user])  
    user.user_role = "teacher" 
    user.teacher.user_id = current_user.id # Work            
    user.teacher.teacher_education.teacher_id = user.teacher.id # Doesn't work 

    if user.save 
    ... 
    end 
end 
end 

ので、user.teacher.teacher_education.teacher_id = user.teacher.idは動作しません。

UPD

エラー

Teacher teacher education teacher can't be blank, Teacher user can't be blank 

ビュー -

<%= 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 %> 

またnew_teacher.html.erb、いくつかのデバッグ情報:

user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
    password: somepass 
    teacher_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
    teacher_birthday: 21.12.1990 
    teacher_category: categ 
    teacher_education_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
     teacher_education_graduation: grad 
     teacher_education_speciality: spec 
     teacher_education_university: univ 
     teacher_education_year: 28.09.2000 
    teacher_first_name: name 
    teacher_last_name: last 
    teacher_middle_name: middle 
    teacher_sex: w 
    user_login: schoolh_Lyp1v 
utf8: ✓ 
controller: admins 

マイスキーマ

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 

答えて

1

ネストされた属性をサポートするために、適切にフォームを構築している場合、これはあなたが必要とするすべてである:

user = User.new(params[:user]) 
user.user_role = "teacher"  
if user.save 
    ... 
end 

accepts_nested_attributes力学は残りの世話をします。上記がうまくいかない場合は、フォームがどのように組み合わされているかを見てみましょう。

+0

私は多くの情報を更新しました。私の投稿をもう一度確認できますか? – ExiRe

+0

私の答えのようにコントローラのアクションを更新しましたか?また、「空ではない」バリデーションはどこから来ますか?上記のモデルコードには何も含まれていません。このような関連付けにbelongs_toに存在チェックを適用しないでください。存在チェックをhas_one関連付けに適用します。 – patrickmcgraw

+0

このようなエラーを発生させるバリデーションを表示するために私のモデル(教師と教師教育)を更新しました。また、私はそこに名前と他のフィールドのバリデーションをたくさん持っています(しかし、私はこれが興味深いとは思いません)。 – ExiRe

関連する問題