mongoid/mongoid — Issue #1433: Trouble with polymorphic relations
Author: tmaier
Created: 2011-11-17 22:06:33+00:00
State: open

Hi guys,

there is an issue I have with polymorpic relations since months and now I need to fix it.

I have the following model and I want to embed TaskValue polymorphic into Task.
My application has a simple form.
Creating a new Task works also with the polymorphic fields but updating a record does not change anything on the embedded (case 3) or related (case 1) `:target`.

Up to now I did not bother because embedding it in the traditional way (case 4) was just fine.

I then tried all possible relations (see below) and also when referencing one TaskValue, it does not update a documents' `:target`.

```ruby
class Task
include Mongoid::Document

# @return [TaskValue] target start and end dates of the activity

# case 1) create works, update fails
#references_one :target, class_name: "TaskValue", as: :timeable, autosave: true

# case 2) create and update works
#references_one :target, class_name: "TaskValue", inverse_of: :task, autosave: true

# case 3) create works, update fails
#embeds_one :target, class_name: "TaskValue", as: :timeable

# case 4) create and update works
embeds_one :target, class_name: "TaskValue", inverse_of: :task

accepts_nested_attributes_for :target
attr_accessible :target_attributes

end
```

```ruby
class TaskValue
include Mongoid::Document

# @return [Time] start date and time
field :start, :type => Time
# @return [Time] end date and time
field :end, :type => Time

# case 1)
#referenced_in :timeable, polymorphic: true

# case 2)
#referenced_in :task, inverse_of: :target

# case 3)
#embedded_in :timeable, polymorphic: true

# case 4)
embedded_in :task, inverse_of: :target
end
```

I would be really happy about any hint what I do wrong.