سؤال

لدي نموذجين:الطالب والمشروع.مشاريع طالب HABTM وطلاب مشروع HABTM.وهنا النموذجين:

class Student < User
    has_many :relationships, dependent: :destroy
    has_many :employers, through: :relationships
    has_and_belongs_to_many :projects, join_table: :projects_students
end

class Project < ActiveRecord::Base
    has_many :relationships
    belongs_to :employer
    has_and_belongs_to_many :students, join_table: :projects_students
end

كما ترون، يستخدم الطالب الوراثة متعددة الأشكال من المستخدم (يحتوي جدول المستخدم على عمود نوع، إحدى القيم هي الطالب).هنا وحدة التحكم التي تنشئ المشاريع:

def create
    @project = current_user.projects.new(project_params)

    respond_to do |format|
        if @project.save
            format.html { redirect_to @project, notice: 'Project was successfully created.' }
            format.json { render action: 'show', status: :created, location: @project }
        else
            format.html { render action: 'new' }
            format.json { render json: @project.errors, status: :unprocessable_entity }
        end
    end
end

def project_params
    params.require(:project).permit(:title, :category, :location, :budget,
       :description, :projectdoc)
end

يتم ربط المشروع والطالب من خلال join_table :projects_students:

create_table "projects_students", force: true do |t|
  t.integer "student_id"
  t.integer "project_id"
end

add_index "projects_students", ["project_id"], name: "index_projects_students_on_project_id", using: :btree
add_index "projects_students", ["student_id"], name: "index_projects_students_on_student_id", using: :btree

تكمن المشكلة في أنه عند إنشاء مشروع، لا يتم تمرير معرف الطالب إلى جدول project_students.كيف يمكنني اصلاح هذا؟

هل كانت مفيدة؟

المحلول

أعتقد أنك تقصد أن تقول إن الطالب يستخدم "ميراث جدول واحد" بدلا من "الوراثة متعددة الأشكال" التي لا يوجد شيء من هذا القبيل.

قالت، نظرا لأن جمعية Habtm هي على المستخدمين وليس الطلاب، فسوف تحقق في وحدة التحكم لمعرفة ما إذا كان هذا يعمل:

giveacodicetagpre.

الشيء التالي الذي أقوم به هو استخدام "Build" بدلا من "جديد" عند إنشاء Instantiating:

giveacodicetagpre.

أفترض أن هذا فقط لإنشاء مشاريع جديدة، وهناك مسار مختلف لإضافة الطلاب إلى المشروعات.

نصائح أخرى

أعتقد أنك تفكر في هذا الأمر بشكل خاطئ.عموما _id محجوز للمفاتيح الخارجية لجدول ذي صلة.وبما أنه لا يوجد Student الجدول، قد تكون القضبان غير قادرة على ربط student_id في جدول الانضمام الخاص بك بشكل مناسب.

حاول تغيير جدول الانضمام الخاص بك:

create_table "projects_users", force: true do |t|
  t.integer "user_id"
  t.integer "project_id"
end

add_index "projects_users", ["project_id"], name: "index_projects_users_on_project_id", using: :btree
add_index "projects_users", ["user_id"], name: "index_projects_users_on_user_id", using: :btree

يجب ألا يهتم جدول الانضمام الخاص بك بإعداد STI الخاص بك.يمكنك التعامل مع ذلك في النماذج الخاصة بك.

إذن فقط قم بتنظيف الارتباطات:

class Student < User
    has_many :relationships, dependent: :destroy
    has_many :employers, through: :relationships
    has_and_belongs_to_many :projects, join_table: :projects_users, foreign_key: :user_id
end

class Project < ActiveRecord::Base
    has_many :relationships
    belongs_to :employer
    has_and_belongs_to_many :students, join_table: :projects_users, class_name: 'Student', association_foreign_key: :user_id
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top