我有一个模型 HighSchool (high_school.rb),它扩展了用户,还有一个模型运动员 (athlete.rb),它也扩展了用户。在 Athlete 中,它有一个 belongs_to :high_school。在高中,它有一个 has_many :athletes。
当尝试从 HighSchool 控制器创建 Athlete 时,在我添加belongs_to 和 has_many 之前它运行良好(因为当时我在 Athlete 中没有 high_school_id 列),它中断了。以下是我收到的错误,任何想法是什么问题?
ActiveRecord::AssociationTypeMismatch (HighSchool(#2194641240) expected, got
String(#2151984260)):
app/controllers/high_school_controller.rb:6:in `new'
app/controllers/high_school_controller.rb:6:in `create_high_school_athlete'
config/initializers/disable_assets_logger.rb:10:in `call'
这是高中控制器:
class HighSchoolController < ApplicationController
def create_high_school_athlete
athlete_attributes = params[:athlete]
athlete_attributes = construct_athlete_attributes(athlete_attributes)
@high_school_athlete = Athlete.new(athlete_attributes)
if @high_school_athlete.valid? && @high_school_athlete.save
stat_success = create_athlete_stats(@high_school_athlete, params)
if stat_success[:success]
respond_to do |format|
format.json { render json: { errors: @high_school_athlete.errors.full_messages } }
end
else
@high_school_athlete.destroy
respond_to do |format|
format.json { render json: { errors: stat_success[:error_messages] } }
end
end
else
respond_to do |format|
format.json { render json: { errors: @high_school_athlete.errors.full_messages } }
end
end
end
def create_athlete_stats high_school_athlete, params
@height = high_school_athlete.stat(:height)
@weight = high_school_athlete.stat(:weight)
height_in_inches = (params[:height_feet][:value].nil? ? 0 : params[:height_feet][:value].to_f * 12) + (params[:height_inches][:value].nil? ? 0 : params[:height_inches][:value].to_f)
@height.value = height_in_inches.to_s
@weight.value = params[:weight][:value]
if @height.save && @weight.save
{:success => true, :error_messages => ""}
else
{:success => false, :error_messages => @height.errors.full_messages + @weight.errors.full_messages}
end
end
private
def construct_athlete_attributes athlete_attributes
athlete_attributes[:username] = create_athlete_username(params[:athlete][:first_name], params[:athlete][:last_name])
athlete_attributes[:password] = (36**(16-1) + rand(36**16)).to_s(36)
athlete_attributes[:password_confirmation] = athlete_attributes[:password]
athlete_attributes[:user_type_id] = 1
athlete_attributes[:account_type_id] = 1
athlete_attributes[:state] = current_high_school.state
athlete_attributes[:hometown] = current_high_school.hometown
athlete_attributes[:high_school] = current_high_school.high_school
athlete_attributes[:high_school_id] = current_high_school.id
athlete_attributes
end
这是一个相当长的 ajax 调用,但直接来自 Ajax 调用的参数(根据 Rails 的日志)如下:
Started POST "/high_school/create_high_school_athlete" for 127.0.0.1 at 2013-04-10 10:45:29 -0500
Processing by HighSchoolController#create_high_school_athlete as */*
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DIzC7Ix45dbXklLvYjbGf9i1sn67amaJYvG6zs5fhUA=",
"athlete"=>{"first_name"=>"a", "last_name"=>"a", "recruit_year"=>"2015",
"primary_sport_id"=>"6", "primary_sport_primary_position_id"=>"",
"secondary_sport_id"=>"", "email"=>"TEST@blah.com", "birthday"=>""}, "height_feet"=>
{"value"=>"5"}, "height_inches"=>{"value"=>"9"}, "weight"=>{"value"=>"160"}}
最后,使用调试器打印运动员属性(就在 Athlete.new 被作为参数调用之前)显示了这一点:
(rdb:1) p athlete_attributes
{"first_name"=>"a", "last_name"=>"a", "recruit_year"=>"2015", "primary_sport_id"=>"6",
"primary_sport_primary_position_id"=>"", "secondary_sport_id"=>"",
"email"=>"TEST@blah.com", "birthday"=>"", "username"=>"aa",
"password"=>"c5n9abih4q705yet", "password_confirmation"=>"c5n9abih4q705yet",
"user_type_id"=>1, "account_type_id"=>1, "state"=>"AL", "hometown"=>"blahtown",
"high_school"=>"Blah University", "high_school_id"=>109}