0

I've created a User model with Devise and added a type column etc., so the models Student and Teacher could inherit from it. All of this worked great. My Teacher model has a one to many relationship to the model Course, where all the data about a teachers courses is stored.

My problem: the Devise helper current_user.courses doesn't work because the courses table has no column user_id. How can I make current_user be able to resolve .courses, even though the attribute in courses is called teacher_id?

I'm a Rails newbie, so any help would be appreciated! :)

EDIT: refined question and added schema and models.

# schema.rb:
create_table "courses", force: :cascade do |t|
    t.string   "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "teacher_id"
    t.index ["teacher_id"], name: "index_courses_on_teacher_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "name"
    t.string   "type"
    t.integer  "quiz_session_id"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["quiz_session_id"], name: "index_users_on_quiz_session_id"
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

# /app/models/course.rb:
class Course < ApplicationRecord
  belongs_to :teacher
  has_many :students

  delegate :teachers, :students, to: :users
end

# /app/models/user.rb:
class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :courses

  # Which users subclass the User model
  def self.types
    %w(Teacher Student)
  end

  # Add scopes to the parent models for each child model
  scope :teachers, -> { where(type: 'Teacher') }
  scope :students, -> { where(type: 'Student') }

end

# /app/models/teacher.rb:
class Teacher < User
end
megahra
  • 295
  • 2
  • 19

3 Answers3

1

You can define your own helpers in this way:

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception
  helper_method :current_teacher, :current_student, 
                :teacher_logged_in?, :student_logged_in?

  private

    def current_teacher
      @current_teacher ||= current_user if user_signed_in? and current_user.class.name == "Teacher"
    end

    def current_student
      @current_student ||= current_user if user_signed_in? and current_user.class.name == "Student"
    end

    def teacher_logged_in?
      @teacher_logged_in ||= user_signed_in? and current_teacher
    end

    def student_logged_in?
      @student_logged_in ||= user_signed_in? and current_student
    end
end

I have not executed these syntaxes but I have written something like this in past so if you face any syntax error then post it in comment.

EDIT:

After seeing your updated model code, I think changing the course association in user model to like below will work for you:

has_many :courses, :foreign_key  => "teacher_id"
Sajan
  • 1,893
  • 2
  • 19
  • 39
  • Thank you, this actually lets me call `current_teacher.courses`, but it gives me the same error as before: `SQLite3::SQLException: no such column: courses.user_id: SELECT "courses".* FROM "courses" WHERE "courses"."user_id" = ?`. I'd prefer a solution, where current_user.courses works for `Teacher` as well as `Student`! – megahra May 13 '17 at 10:23
  • One more question: I would like `current_user.courses` to work no matter if I'm logged in as a Student or as a Teacher. Rght now it should only work for the Teacher model, since I only added `=> "teacher_id"`, how do I have to change it so it would also work for `"student_id"`? @sajan – megahra May 13 '17 at 11:25
  • Oh, sorry, additional info: Student - Course is a many to many relationship expressed by a table called `"courses_students"` containing the respective `course_id` and `student_id` of each record. – megahra May 13 '17 at 11:29
  • I suppose you have a `has_many :courses` in `Student` class. is it not working ? any error ? did you try adding same `foreign_key` option `student_id` to this association ? – Sajan May 13 '17 at 12:27
  • No, the Student model inherits the `has_many :courses` relationship from the User model. There are no additional relationships defined in the Student model. If I add the lines `has_many :courses, :foreign_key => "teacher_id"` and `has_many :courses, :foreign_key => "student_id"` to the User model, only the second line works. – megahra May 13 '17 at 20:01
  • My question is: when calling `current_user.courses` on a user of the type Student, how does the model know it needs to look in the table `courses_students` to select all the courses for the current user? – megahra May 13 '17 at 20:07
  • do you know? @sajan – megahra May 14 '17 at 08:02
  • Ofcourse you cant use same association twice with just a different foreign key. And since you are storing the association record in a different table and it is a many to many relation, I think you should use has_and_belongs_to_many association. check the rails guide for this association. – Sajan May 14 '17 at 08:41
0

Run:

rails generate migration AddUserReferenceToCourses user:references

I think that will create a migration that will add user_id to courses

code4fun
  • 166
  • 1
  • 2
  • 11
  • Oh, I wasn't quite clear, sorry: it's intentional that the relationship is between Teacher and Courses, since the other class that inherits from User (model Student) has a different relationship to Courses. – megahra May 13 '17 at 10:19
0

You can add a migration to the courses table:

rails g migration add_user_id_to_courses user:references

This will add belongs_to :user in the course.rb file. Then go to the user.rb file and add:

has_many :courses

This relation will allow you to call the user from a course or a course from a user.

hashrocket
  • 2,210
  • 1
  • 16
  • 25