2

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?

ExiRe
  • 4,727
  • 7
  • 47
  • 92
  • What error are you getting? What does your view look like? – Jordan Running Mar 15 '12 at 13:23
  • Added the text of error in my post. – ExiRe Mar 15 '12 at 13:26
  • You create a teacher from an existing User? Or do you create the user in here too? What I mean is, do transform a user into a teacher? – Ismael Abreu Mar 15 '12 at 13:33
  • I try to create user AND teacher. – ExiRe Mar 15 '12 at 13:34
  • What params does it show on the error page? – Ed Jones Mar 15 '12 at 13:37
  • Also, it looks like you have no teacher.save, so you won't be creating both? – Ed Jones Mar 15 '12 at 13:45
  • I updated my post, and now you can see my params. – ExiRe Mar 15 '12 at 13:58
  • I WANT to create user and teacher in same time bu `User.new(params[:user])` thats why i give User model access to teacher_attributes. – ExiRe Mar 15 '12 at 14:02
  • Ah, but there are no teacher attributes, because there is no teacher yet! I get your frustration here. It seems very natural to create a type of user and in the process create the underlying user instance. I wrote the same questin here: http://stackoverflow.com/questions/9006918/rails-user-heirarchies-classes-associations-polymorphic – Ed Jones Mar 15 '12 at 14:16

3 Answers3

1

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!

Ed Jones
  • 653
  • 1
  • 4
  • 19
  • Ow! Your way with 2 different forms makes sense! I thought that there was easy way to save 2 models at once... Anyway, thanks for your help! – ExiRe Mar 15 '12 at 14:26
  • Well, thanks-sortof! You bumped me to two new privaledges. But... I'd really like to know how to do what you were trying! I want a teacher to be able to sign up, and add teacher-specific info at the same time, because I also have student users. – Ed Jones Mar 15 '12 at 14:37
  • 1
    I think, i found how to do that. I can share with you my result. You can write me your email if you want. – ExiRe Mar 15 '12 at 17:58
  • lol, no, 'cool' was not part of the email. It's just plain 'ed.jones@gmail.com' or if you prefer, edjones@openhistoryproject.org – Ed Jones Mar 16 '12 at 00:53
0

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

hlcfan
  • 313
  • 1
  • 2
  • 10
  • once i encounter this error,while i changed to params[:user]['user_role'] = "teacher",it works.just experience. – hlcfan Mar 15 '12 at 13:25
0

Edit: As I said in the comment:

Try:

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

instead of User.new

Albin
  • 2,912
  • 1
  • 21
  • 31
  • As i understand, `user = User.new(params[:user])` should create a teacher because it have access to `teacher_attribute` in User model. Problem is that i don't know how add user_id for a teacher correctly. – ExiRe Mar 15 '12 at 13:30
  • The user does not have an id until you save it. Im not sure how it works with accessible but you do not include user_id in your list of accessible attributes in teacher – Albin Mar 15 '12 at 13:34
  • 1
    I think maybe you should use User.create(params) instead of User.new(params), I think it saves them right away and that is how they do it on http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html – Albin Mar 15 '12 at 13:37
  • Problem is that it cannot be saves because teacher needs user_id. – ExiRe Mar 15 '12 at 14:04
  • 1
    Is the user_id attribute accessible to you user model? – Albin Mar 15 '12 at 14:14
  • No, because i don't think that this is safe. – ExiRe Mar 15 '12 at 14:21