2017-05-13 4 views
0

私は、DeviseでUserモデルを作成し、typeカラムなどを追加しました。したがってモデルStudentTeacherはそれを継承する可能性があります。このすべてがうまくいった。私のTeacherモデルは、モデルCourseと1対多の関係にあり、教師コースに関するすべてのデータが保存されています。Railsシングルテーブル継承ヘルパーを作成する

私の問題:coursesテーブルに列がないため、Devitsヘルパーcurrent_user.coursesが機能しませんuser_id。コースの属性がteacher_idであっても、current_user.coursesを解決できるようにするにはどうすればよいですか?

私はRailsの初心者ですから、どんな助けもありがとうございます! :)

EDIT:洗練された質問と追加されたスキーマとモデル。

# 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 
+0

あなたは 'schema.rb'を追加することができますし、 /またはモデル? –

答えて

1

あなたは、このように、独自のヘルパーを定義することができます。

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 

私は、これらの構文を実行していないが、あなたは任意の構文エラーに直面した場合、その後のコメントでそれを投稿ので、私は過去にこのような何かを書かれています。

EDIT:

あなたの更新されたモデルのコードを見た後、私は以下好きにユーザモデルにcourse関連付けを変更すると、あなたのために働くだろうと思う:

has_many :courses, :foreign_key => "teacher_id" 
+0

ありがとう、これは実際には 'current_teacher.courses'を呼び出すことができますが、前と同じエラーが表示されます:' SQLite3 :: SQLException:そのような列はありません:courses.user_id:SELECT "courses"。* FROM "courses" WHERE "コース"。 "user_id" =? '。私はcurrent_user.coursesが 'Teacher'と' Student'の両方で動作するソリューションを好むでしょう! – megahra

+0

それは動作します!もう一度学びました:Dありがとう! – megahra

+0

もう1つ質問:「current_user.courses」は、私が学生として、または教師としてログインしていても動作します。今度は '=>" teacher_id "'を追加しただけなので、教師モデルのために働くべきです。student_id "'のためにどのように変更する必要がありますか? @sajan – megahra

0

ラン:

rails g migration add_user_id_to_courses user:references 

rails generate migration AddUserReferenceToCourses user:references 

は、私はそれはあなたがコーステーブルへの移行を追加することができます

+0

ああ、私ははっきりしていませんでした。申し訳ありません。ユーザ(モデル学生)から継承する他のクラスはコースとは異なる関係を持っているため、教師とコースの関係が意図的です。 – megahra

0

コースにuser_idを追加する移行を作成すると思いますこれにより、course.rbファイルにbelongs_to :userが追加されます。次に、user.rbファイルに移動して追加します。

has_many :courses 

この関係では、ユーザーのコースまたはコースからユーザーを呼び出すことができます。

関連する問題