Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:36:00+00:00 2026-05-31T18:36:00+00:00

I have two models: User and Teacher : class User < ActiveRecord::Base attr_accessor :password

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:36:01+00:00Added an answer on May 31, 2026 at 6:36 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two models set up like this: class User < ActiveRecord::Base #
Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
Hey, need a little help here. I have two models: class User < ActiveRecord::Base
I have two models: User and Store class Store < ActiveRecord::Base belongs_to :user class
so I have two models: class User < ActiveRecord::Base has_and_belongs_to_many :followed_courses, :class_name => Course
I have two models class User < ActiveRecord::Base has_many :requests, :foreign_key => :recipient_id .
I have two models. class User has_one :spec, :dependent => :destroy # returns true
I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
I have two models class Subscription < ActiveRecord::Base belongs_to :client end class Client <

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.