简体   繁体   English

Rails HABTM关系不适用于多态关系

[英]Rails HABTM relationship not working with polymorphic relationship

I have two models: Student and Project. 我有两个模型:Student和Project。 A student HABTM projects and a Project HABTM students. HABTM学生项目和HABTM项目学生。 Here are the two models: 这是两个模型:

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

As you can see, Student uses polymorphic inheritance from User (The user table has a type column, one of the values is Student). 如您所见,Student使用来自User的多态继承(user表有一个type列,值之一是Student)。 Here is the controller that creates projects: 这是创建项目的控制器:

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

Project and Student are connected through the join_table :projects_students: Project和Student通过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

The problem is that when a project is created, the student_id is not being passed to the projects_students table. 问题在于创建项目时,student_id不会传递到projects_students表。 How can I fix this? 我怎样才能解决这个问题?

I think you mean to say that Student uses "single table inheritance" rather than "polymorphic inheritance" of which there is no such thing. 我想您的意思是说Student使用的是“单表继承”而不是“多表继承”,而后者没有这种东西。

That said, since the HABTM association is on users and not students, I'd check in the console to see if this works: 就是说,由于HABTM关联是针对用户而非学生的,因此我将在控制台中检查它是否有效:

Student.first.projects << Project.first

Next thing I'd do is to use "build" instead of "new" when instantiating the project: 我接下来要做的是在实例化项目时使用“ build”而不是“ new”:

@project = current_user.projects.build project_params

I suppose this is only for creating new projects, and that there is a different route for adding students to projects. 我想这仅是用于创建新项目,并且有一条将学生添加到项目中的不同途径。

I think you're thinking about this wrong. 我认为您正在考虑这个错误。 Generally the _id is reserved for foreign keys to a related table. 通常, _id保留给相关表的外键。 Since there is no Student table, rails may be unable to link the student_id in your join table appropriately. 由于没有Student表,因此rails可能无法正确链接student_id表中的student_id

Try changing your join table: 尝试更改您的联接表:

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

Your join table shouldn't care about your STI setup. 您的联接表应该不在乎您的STI设置。 You can handle that in your models. 您可以在模型中处理。

So then just cleanup the associations: 因此,只需清理关联:

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM