ExiRe
ExiRe

Reputation: 4767

Rails 3 - correct way to give id for belongs_to model

I have two models: User and Teacher:

class User < ActiveRecord::Base
  attr_accessor   :password                                                               
  attr_accessible :user_login,                                                           
                  :user_role,
                  :password,
                  :teacher_attributes

  has_one :teacher
  accepts_nested_attributes_for :teacher                                                  
end

# Table name: teachers
#
#  id                  :integer         not null, primary key
#  user_id             :integer
#  teacher_last_name   :string(255)
#  teacher_first_name  :string(255)
#  teacher_middle_name :string(255)
#  teacher_birthday    :date
#  teacher_sex         :string(255)
#  teacher_category    :string(255)
#  created_at          :datetime
#  updated_at          :datetime

class Teacher < ActiveRecord::Base
  attr_accessible :teacher_last_name,
                  :teacher_first_name,
                  :teacher_middle_name,
                  :teacher_birthday,
                  :teacher_sex,
                  :teacher_category

  belongs_to :user
end

Also I have a method in my controller which should create user and then new teacher. But doesn't work correctly, because I don't pass an id there.

My controller

class AdminsController < ApplicationController
  def create_teacher
    user_errors, teacher_errors, redirect_path = nil, nil, nil 

    params[:user][:user_role] = "teacher"
    user = User.new(params[:user])                           #Problem is here

    if user.save
      redirect_path = admins_users_of_system_path
      flash[:success] = "Teacher created!"
    else
      redirect_path = admins_new_teacher_path
      user_errors = user.errors.full_messages.to_sentence                                     
      teacher_errors = user.teacher.errors.full_messages.to_sentence if user.teacher
    end

    errors_arr = [user_errors, teacher_errors].compact
    flash[:error] = errors_arr if errors_arr.present? 
    redirect_to redirect_path
  end
end

I have such code in controller because i want to grab all errors from user and teacher.

Error

["Teacher user can't be blank", "User can't be blank"]

utf8: ✓
authenticity_token: Hsw9BGOMb37cku6R2aAZsXkYaU5DFZqlUML4yZjqxI0=
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  teacher_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    teacher_last_name: Popov
    teacher_first_name: Igor
    teacher_middle_name: Yurevich
    teacher_sex: m
    teacher_birthday: 28.08.1991
    teacher_category: Something 1
  user_login: schoolh_MXWTQ
  password: eviRDyalXnHlK6q
  user_role: teacher
commit: Создать
controller: admins
action: create_teacher

How can I create a teacher and an user in same time correctly? How should I pass it an id?

Upvotes: 2

Views: 507

Answers (3)

Ed Jones
Ed Jones

Reputation: 653

A different approach to this is to simply create the User on the first go round, including the role of teacher. Then the admin fills in a second form with the profile details, a "Fill in your profile" if you will (teacher model would still contain this.)

Either way, you'll probably want the delegates_to method for accessing methods http://apidock.com/rails/v3.1.0/Module/delegate

Also, however you do it, you might just consider leaving the attr_accessible out until the thing is otherwise working properly.

But if you find a way to create them both at once, I'll be the first to copy you!

Upvotes: 1

Albin
Albin

Reputation: 3032

Edit: As I said in the comment:

Try:

user = User.create(params[:user])

instead of User.new

Upvotes: 0

hlcfan
hlcfan

Reputation: 333

i think prob is params u give got wrong. right way should be:params[:user]['user_role'] = "teacher"

Upvotes: 0

Related Questions