我一直致力于实施属于我们的某个网络应用现有模型的新模型。最终,我想构建一个嵌套的表单。我知道表单和强大的params可以拥有它自己的一系列问题,但我现在正在努力让模型像我在rails控制台中所期望的那样运行。
Rails 4.2.7,Postgres DB
更新 - 2016年10月3日 - 仍在努力寻找合适的解决方案,但已做出一些改变
我们的工作是与学校和学区合作,这个特殊案例涉及调查以及如何将调查分配给学校和学区。到目前为止,一项调查已经分配给一个带有SurveyAssignment模型的地区,并且有一些调查逻辑假设一个地区的所有学校也被分配了#34;调查。现在,我们希望能够为SurveyAssignment添加更多粒度,并在学校级别允许一些特异性。
所以我创建了一个SchoolSurveyAssignment模型并开始使用这些位。
以下是相关的型号信息:
class District < ActiveRecord::Base
...
has_many :schools, dependent: :destroy
has_many :survey_assignments, dependent: :destroy
...
end
class School
...
belongs_to :district
has_many :school_survey_assignments
has_many :survey_assignments, :through => :school_survey_assignments
...
end
class SurveyAssignment
belongs_to :district
belongs_to :survey
has_one :survey_version, through: :survey
has_many :school_survey_assignments, inverse_of: survey_assignment
has_many :schools, :through => :school_survey_assignments
accepts_nested_attributes_for :school_survey_assignments
attr_accessor :survey_group, :survey_version_type, :survey_version_id, :school_survey_assignments_attributes
validates :survey_id, presence: true
end
class SchoolSurveyAssignment
belongs_to :survey_assignment, inverse_of: :school_survey_assignments
belongs_to :school
attr_accessor :school_id, :survey_assignment_id, :grades_affected, :ulc_affected
validates_presence_of :survey_assignment
validates :school_id, presence: true, uniqueness: {scope: :survey_assignment_id}
end
相关控制器代码:
class SurveyAssignmentsController < ApplicationController
before_action :set_district
before_action :set_survey_assignment, only: [:show, :edit, :update, :destroy]
respond_to :html, :json, :js
def new
@new_survey_assignment = SurveyAssignment.new()
@district.schools.each do |school|
@new_survey_assignment.school_survey_assignments.build(school_id: school.id)
end
end
def create
@survey_assignment = SurveyAssignment.new(survey_assignment_params)
if @survey_assignment.save
flash[:notice] = "Survey successfully assigned to #{@district.name}"
else
flash[:alert] = "There was a problem assigning this survey to #{@district.name}"
end
redirect_to district_survey_assignments_path(@district)
end
def survey_assignment_params
params.require(:survey_assignment).permit(:survey_id, :status, :survey_version_id, school_survey_assignments_attributes: [:id, :survey_assignment_id, :school_id, grades_affected: [], ulc_affected: []]).tap do |p|
p[:district_id] = @district.id
p[:school_year] = session[:selected_year]
end
end
def set_district
@district = District.find(params[:district_id])
end
以下是相关的架构信息:
create_table "school_survey_assignments", force: :cascade do |t|
t.integer "survey_assignment_id"
t.integer "school_id"
t.integer "grades_affected", default: [], array: true
t.string "ulc_affected", default: [], array: true
end
add_index "school_survey_assignments", ["school_id"], name: "index_school_survey_assignments_on_school_id", using: :btree
add_index "school_survey_assignments", ["survey_assignment_id"], name: "index_school_survey_assignments_on_survey_assignment_id", using: :btree
create_table "survey_assignments", force: :cascade do |t|
t.integer "district_id"
t.integer "survey_id"
t.integer "status"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "school_year"
t.integer "last_response_status_id"
end
add_index "survey_assignments", ["district_id"], name: "index_survey_assignments_on_district_id", using: :btree
一旦这些到位,我就进入我的rails控制台并尝试以下方法:
2.3.1 :002 > sa1 = SurveyAssignment.create(district_id: 3, survey_id: 508, school_year: 2017)
(0.2ms) BEGIN
SQL (0.7ms) INSERT INTO "survey_assignments" ("district_id", "survey_id", "school_year", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["district_id", 3], ["survey_id", 508], ["school_year", 2017], ["created_at", "2016-09-30 21:30:20.205144"], ["updated_at", "2016-09-30 21:30:20.205144"]]
(7.2ms) COMMIT
=> #<SurveyAssignment id: 369, district_id: 3, survey_id: 508, status: nil, created_at: "2016-09-30 21:30:20", updated_at: "2016-09-30 21:30:20", school_year: 2017, last_response_status_id: nil>
2.3.1 :003 > sa2 = SurveyAssignment.create(district_id: 3, survey_id: 508, school_year: 2017)
(0.3ms) BEGIN
SQL (0.4ms) INSERT INTO "survey_assignments" ("district_id", "survey_id", "school_year", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["district_id", 3], ["survey_id", 508], ["school_year", 2017], ["created_at", "2016-09-30 21:30:30.701197"], ["updated_at", "2016-09-30 21:30:30.701197"]]
(0.5ms) COMMIT
=> #<SurveyAssignment id: 370, district_id: 3, survey_id: 508, status: nil, created_at: "2016-09-30 21:30:30", updated_at: "2016-09-30 21:30:30", school_year: 2017, last_response_status_id: nil>
现在,我已经成功创建了两个调查任务。我现在要从sa1创建两个学校调查任务:
2.3.1 :004 > [{school_id: 5}, {school_id: 6}].each do |ssa|
2.3.1 :005 > sa1.school_survey_assignments.create(ssa)
2.3.1 :006?> end
(0.2ms) BEGIN
SchoolSurveyAssignment Exists (2.4ms) SELECT 1 AS one FROM "school_survey_assignments" WHERE ("school_survey_assignments"."school_id" = 5 AND "school_survey_assignments"."survey_assignment_id" = 369) LIMIT 1
SQL (0.4ms) INSERT INTO "school_survey_assignments" ("survey_assignment_id") VALUES ($1) RETURNING "id" [["survey_assignment_id", 369]]
(6.4ms) COMMIT
(0.6ms) BEGIN
SchoolSurveyAssignment Exists (0.4ms) SELECT 1 AS one FROM "school_survey_assignments" WHERE ("school_survey_assignments"."school_id" = 6 AND "school_survey_assignments"."survey_assignment_id" = 369) LIMIT 1
SQL (0.3ms) INSERT INTO "school_survey_assignments" ("survey_assignment_id") VALUES ($1) RETURNING "id" [["survey_assignment_id", 369]]
(0.4ms) COMMIT
=> [{:school_id=>5}, {:school_id=>6}]
2.3.1 :007 > sa1.save
(0.3ms) BEGIN
(0.4ms) COMMIT
=> true
现在,看起来我已经成功创建了两个SchoolSurveyAssignments,其中survey_assignment_id = 369,school_ids = 5和6
2.3.1 :008 > sa1.school_survey_assignments
SchoolSurveyAssignment Load (0.3ms) SELECT "school_survey_assignments".* FROM "school_survey_assignments" WHERE "school_survey_assignments"."survey_assignment_id" = $1 [["survey_assignment_id", 369]]
=> #<ActiveRecord::Associations::CollectionProxy [#<SchoolSurveyAssignment id: 5, survey_assignment_id: 369, school_id: nil, grades_affected: [], ulc_affected: []>, #<SchoolSurveyAssignment id: 6, survey_assignment_id: 369, school_id: nil, grades_affected: [], ulc_affected: []>]>
正如您从ActivRecord :: Associations :: CollectionProxy中看到的那样,两个SchoolSurveyAssignments都是使用survey_assignment_id:369创建的,但是使用了nil school_id。这似乎是令人不安的
我不理解的其他项目如下:
2.3.1 :009 > SchoolSurveyAssignment.find(5).survey_assignment_id
SchoolSurveyAssignment Load (0.6ms) SELECT "school_survey_assignments".* FROM "school_survey_assignments" WHERE "school_survey_assignments"."id" = $1 LIMIT 1 [["id", 5]]
=> nil
2.3.1 :011 > SchoolSurveyAssignment.find(5).survey_assignment.id
SchoolSurveyAssignment Load (0.3ms) SELECT "school_survey_assignments".* FROM "school_survey_assignments" WHERE "school_survey_assignments"."id" = $1 LIMIT 1 [["id", 5]]
SurveyAssignment Load (0.4ms) SELECT "survey_assignments".* FROM "survey_assignments" WHERE "survey_assignments"."id" = $1 LIMIT 1 [["id", 369]]
=> 369
调用.survey_assignment_id应该返回SchoolSurveyAssignment上的属性并给出369. .survey_assignment.id只是抓取父对象的ID。我希望两者都返回相同的值,但是返回nil。
最终用例是制作一个SurveyAssignment表单,该表单允许用户设置新SurveyAssignment的属性,并设置X个SchoolSurveyAssignments的属性(基于区中的学校数量;从2到15不等)。一旦我更好地掌握了这些模型的交互方式,我对实现这一目标充满信心,但我所看到的行为对我来说没有意义,我希望能够找到一些明确的实现方式。这些相关的模型。我觉得我在答案中蹦蹦跳跳,但我错过了一个关键的细节。
谢谢,
亚历
答案 0 :(得分:1)
对于第一个问题,School和SurveyAssignment彼此不认识,school_id变为零。在你的应用程序中,这些模型间接具有m到n关联,那么如何通过模型之间的关联使用has_many呢?
在学校模型中,请添加以下内容:
Publisher(
PublisherID (PK),
Name,
Address
)
Book(
BookID (PK),
Name,
Author,
Price,
PublisherID (FK on Publisher)
)
在SurveyAssignments模型中,添加以下内容:
has_many :survey_assignments, :through => :school_survey_assignments
对于最后一个问题,两个代码似乎都相同..
答案 1 :(得分:1)
尝试删除attr_accessor
行代码。 attr_accessor
不应该用于数据库中持久存在的属性,它可能会搞乱ActiveRecord默认提供的方法,导致这些属性无法正确保存
class SurveyAssignment
belongs_to :district
belongs_to :survey
has_one :survey_version, through: :survey
has_many :school_survey_assignments, inverse_of: survey_assignment
has_many :schools, :through => :school_survey_assignments
accepts_nested_attributes_for :school_survey_assignments
validates :survey_id, presence: true
end
class SchoolSurveyAssignment
belongs_to :survey_assignment, inverse_of: :school_survey_assignments
belongs_to :school
validates_presence_of :survey_assignment
validates :school_id, presence: true, uniqueness: {scope: :survey_assignment_id}
end