How to maintain a many-to-many relationship?

I am trying to create a form to maintain a rather complex relationship. Models for relationships below.

class Goal < ActiveRecord::Base
  belongs_to :goal_status
  belongs_to :goal_type

  has_many :users, through: :user_goals
  has_many :user_goals

  has_many :parent_goals, class_name: 'GoalDependency', foreign_key: :parent_id
  has_many :child_goals, class_name: 'GoalDependency', foreign_key: :child_id

  has_many :children, through: :child_goals
  has_many :parents, through: :parent_goals

  validates_presence_of :goal_status_id, :goal_type_id
end

class GoalDependency < ActiveRecord::Base
  belongs_to :parent, class_name: 'Goal', foreign_key: 'parent_id'
  belongs_to :child, class_name: 'Goal', foreign_key: 'child_id'
end

Thus, the goal may have many parents or may have many children, or both. I tried to use the drop-down list of several elements to preserve these relationships and set child_ids / parent_ids, but this does not work because both dependency fields are required for the dependency_ target - that is, child_id and parent_id. Rails installs only one. Therefore, if I save the list of child_ids, it sets them, but it does not know to set parent_id with the current target and vice versa.

I tried using accepts_nested_attributes, but I'm not quite sure how I can use this with a multiple selection dropdown.

, , .

.

.row
  .col-md-6
    = simple_form_for(@goal) do |f|
      - if @goal.errors.any?
        #error_explanation
          h2 = "#{pluralize(@goal.errors.count, 'error')} prohibited this goal from being saved:"

          ul
            -@goal.errors.full_messages.each do |message|
              li = message

      = f.input :description
      = f.input :goal_status_id, collection: @goal_statuses, value_method: :id, label_method: :name, as: :select
      = f.input :goal_type_id, collection: @goal_types, value_method: :id, label_method: :name, as: :select
      = f.input :user_ids, collection: @users, as: :select, label: 'Assigned To', input_html: { class: 'chosen-select', multiple: true }, selected: @goal.user_ids
      = f.input :child_ids, collection: @goals, as: :select, label: 'Children Goals', input_html: { class: 'chosen-select', multiple: true }, selected: @goal.child_ids, value_method: :id, label_method: :description

      br
      = f.submit

, . . -. . .

class Goal < ActiveRecord::Base
  belongs_to :goal_status
  belongs_to :goal_type

  has_many :users, through: :user_goals
  has_many :user_goals

  has_many :child_goals, class_name: 'GoalChildDependency'
  has_many :children, through: :child_goals

  validates_presence_of :goal_status_id, :goal_type_id
end

class GoalChildDependency < ActiveRecord::Base
  belongs_to :goal
  belongs_to :child, class_name: 'Goal', foreign_key: :child_id
end

, , , , .

, - , , .

+4
1

, parent_id .

has_many :child_goals, class_name: 'Goal', foreign_key: :parent_id

belongs_to :parent_goal, class_name: 'Goal'

, ChildGoal, , type ChildGoal < Goal , .

0

All Articles