Имам два модела: Студентски и Проектен. Студентски проекти 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
Както можете да видите, Student използва полиморфно наследяване от User (Потребителската таблица има колона тип, една от стойностите е Student). Ето контролера, който създава проекти:
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
Проблемът е, че когато се създава проект, student_id не се предава на таблицата projects_students. Как мога да поправя това?