У меня есть модель Tournament
с ассоциацией с Category
:
has_one :category_id, class_name: "Category", optional: true
Схема ее базы данных:
create_table "tournaments", force: :cascade do |t|
t.string "name"
t.datetime "start_at"
t.datetime "end_at"
# ...
t.integer "category_id"
end
и по какой-то причине я получаю ошибка несоответствия типа после отправки формы создания:
<div class="mt-4">
<%= f.label "Select tournament game", class: "block text-sm font-medium text-gray-700" %>
<div class="mt-1 rounded-md shadow-sm">
<%= f.select(:category_id, Category.all.map{|c| [c.name, c.id]}, {prompt: true, selected: :category_id}, {class: "ts_select block form-select w-full transition duration-150 ease-in-out sm:text-sm sm"}) %>
</div>
</div>
Вот ошибка и данные, переданные при выполнении запроса
Started POST "/tournaments" for ::1 at 2020-07-10 00:51:10 +0100
Processing by TournamentsController#create as HTML
Parameters: {"authenticity_token"=>"s7itTc9N+7UkNjYGjkukaZ51NvS3ZfVnrpvLC2xl2umCHsypJN/clhezKX7JZ5bPsZfcUVXeFH9FCJroLkoeZg==", "tournament"=>{"name"=>"test tournament", "country"=>"US", "start_at(3i)"=>"9", "start_at(2i)"=>"7", "start_at(1i)"=>"2020", "start_at(4i)"=>"23", "start_at(5i)"=>"50", "end_at(3i)"=>"9", "end_at(2i)"=>"7", "end_at(1i)"=>"2020", "end_at(4i)"=>"23", "end_at(5i)"=>"50", "featured_player_id"=>"", "stream_link"=>"nickmercs", "highlight_clip"=>"SneakyBenevolentFennelGOWSkull", "prize"=>"20000", "category_id"=>"1"}, "commit"=>"Create Tournament"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.4ms | Allocations: 2814)
ActiveRecord::AssociationTypeMismatch (Category(#35300) expected, got "1" which is an instance of String(#5180)):
activerecord (6.0.3.2) lib/active_record/associations/association.rb:283:in `raise_on_type_mismatch!'
activerecord (6.0.3.2) lib/active_record/associations/belongs_to_association.rb:66:in `replace'
activerecord (6.0.3.2) lib/active_record/associations/singular_association.rb:17:in `writer'
activerecord (6.0.3.2) lib/active_record/associations/builder/association.rb:108:in `category_id='
...
Я проверил все экземпляры категории «Категория "и" category_id "и только category_id
используется в турнирах.