我正在寻求有关创建和保存对象的帮助。 我有三个相关的模型,WorkoutType,ExerciseType和WeightUnit:
class WorkoutType < ActiveRecord::Base
attr_accessor :exercise_type_attributes, :workout_type_id, :weight_unit_id
has_many :exercise_types, dependent: :destroy
belongs_to :user
accepts_nested_attributes_for :exercise_types
end
class ExerciseType < ActiveRecord::Base
attr_accessor :workout_type_id, :weight_unit_id
belongs_to :workout_type
has_many :exercises
belongs_to :weight_unit
end
class WeightUnit < ActiveRecord::Base
has_many :exercise_types
end
这些是相关的表模式:
create_table "workout_types", force: :cascade do |t|
t.string "type_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.boolean "public"
t.text "description"
t.boolean "is_visible"
end
create_table "exercise_types", force: :cascade do |t|
t.string "name"
t.integer "sets"
t.integer "reps"
t.integer "workout_type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "load"
t.string "url"
t.integer "weight_unit_id"
end
create_table "weight_units", force: :cascade do |t|
t.string "name"
t.decimal "conversion_factor_to_lb", precision: 10, scale: 4
end
使用包含this image中所示的ExerciseTypes和WeightUnits的表单来创建WorkoutType。
在WorkoutType
的{{1}}方法中创建了一个新的new
:
WorkoutTypesController
使用create方法保存它:
def new
@workout_type = WorkoutType.new
@exercise_types = Array.new(10) { @workout_type.exercise_types.build }
end
问题是,当我保存新的def create
@workout_type = current_user.workout_types.create(workout_type_params)
@exercise_types = @workout_type.exercise_types
end
def workout_type_params
params.require(:workout_type).permit(:type_name, :public, :description, :exercise_types_attributes => [:id, :name, :sets, :reps, :load, :url, :weight_unit_id])
end
时,WorkoutType
的{{1}}字段不会保存。在终端中,我看到它通过表单作为参数传递:
weight_unit_id
但是,当需要将时间保存到数据库中时,我发现ExerciseTypes
语句中没有包含Started POST "/workout_types" for 10.0.2.2 at 2019-10-07 22:01:22 +0000
Processing by WorkoutTypesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "workout_type"=>{"type_name"=>"Test Routine 10", "description"=>"<p>uygyu</p>", "public"=>"true", "exercise_types_attributes"=>{"0"=>{"name"=>"exercise 1", "sets"=>"2", "reps"=>"34", "load"=>"90", "weight_unit_id"=>"2", "url"=>""},
}}
:
weight_unit_id
保存后,WorkoutType的INSERT INTO "exercise_types"
为 (0.0ms) BEGIN
SQL (0.0ms) INSERT INTO "workout_types" ("type_name", "public", "description", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["type_name", "Test Routine 9"], ["public", "t"], ["description", "<p>great new description</p>"], ["user_id", 1], ["created_at", "2019-10-07 22:01:24.429003"], ["updated_at", "2019-10-07 22:01:24.429003"]]
SQL (0.0ms) INSERT INTO "exercise_types" ("name", "sets", "reps", "load", "url", "workout_type_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["name", "exercise 1"], ["sets", 2], ["reps", 34], ["load", "90"], ["url", ""], ["workout_type_id", 23], ["created_at", "2019-10-07 22:11:11.522873"], ["updated_at", "2019-10-07 22:11:11.522873"]]
。我猜想我需要在create方法中创建WeightUnits对象。但是我该怎么办呢?