V
V
Vsevolod Rodionov2014-02-11 21:05:19
ruby
Vsevolod Rodionov, 2014-02-11 21:05:19

Mongoid for some reason does not pick up the reverse many-to-many self-association of the model?

The model looks like this in a nutshell:

class Task
  include Mongoid::Document
  has_and_belongs_to_many :dependent_tasks, class_name: "Task", inverse_of: :depending_tasks
  has_and_belongs_to_many :depending_tasks, class_name: "Task", inverse_of: :dependent_tasks
  attr_accessible :dependent_tasks, :depending_tasks
end

But for some reason it looks like this:
task.depending_tasks << task2 #=> [#<Task _id: 52fa6127e826a69ea2000002, parent_id: nil, dependent_task_ids: ["52fa6123e826a69ea2000001"], depending_task_ids: nil>]

task2 #=> #<Task _id: 52fa6127e826a69ea2000002, parent_id: nil, dependent_task_ids: ["52fa6123e826a69ea2000001"], depending_task_ids: nil>

task2.dependent_tasks #=> []
task2.dependent_task_ids #=> ["52fa6123e826a69ea2000001"]

Judging by the fact that dependent_task_ids exists, it looks like the problem is in mongoid.
How can it be corrected?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question