Skip to content

Rails 5 polymorphic associations not saving #23960

@petermumford

Description

@petermumford

Hello. I am trying to create a polymorphic association with a has_many :through for categories.

Steps to reproduce

I have created two very simple repositories one with Rails 5.0.0.beta3 and the other using Rails 4.1.13, which works as expected.

Expected behavior

When you create an event with multiple categories selected it should create the association between the event and category on the categorisations model.

Actual behavior

However, when saving the event with multiple categories selected I get the following error: @messages={:categorisations=>["is invalid"]} when using Rails 5.0.0.beta3

Below is the output when debugging from byebug:

(byebug) event_params <ActionController::Parameters {"name"=>"Test", "location"=>"london", "category_ids"=>["", "1", "2"]} permitted: true>

`(byebug) @event

<Event id: nil, name: "Test", location: "london", created_at: nil, updated_at: nil>`

`(byebug) @event.categorisations

<ActiveRecord::Associations::CollectionProxy [#<Categorisation id: nil, category_id: 1, categorisable_type: "Event", categorisable_id: nil, created_at: nil, updated_at: nil>, #<Categorisation id: nil, category_id: 2, categorisable_type: "Event", categorisable_id: nil, created_at: nil, updated_at: nil>]>`

`(byebug) @event.categories

<ActiveRecord::Associations::CollectionProxy [#<Category id: 1, name: "Category 1", created_at: "2016-02-28 21:51:16", updated_at: "2016-02-28 21:51:16">, #<Category id: 2, name: "Category 2", created_at: "2016-02-28 21:51:24", updated_at: "2016-02-28 21:51:24">]>`

(byebug) @event.valid? false

`(byebug) @event.errors

<ActiveModel::Errors:0x007fa113eda628 @base=#<Event id: nil, name: "Test", location: "london", created_at: nil, updated_at: nil>, @messages={:categorisations=>["is invalid"]}, @details={:categorisations=>[{:error=>:invalid}, {:error=>:invalid}]}>`

System configuration

Rails version: 5.0.0.beta3
Ruby version: 2.2.3

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions