title: How to access a model that belongs_to 2 models with all 3 models nested under another model?
body: <p>I'm modelling a decision matrix, so for each <strong>Decision</strong> (n of them), there are x <strong>Alternatives</strong> to choose from and y <strong>Goals</strong> to meet. Each of x*y pairings of Alternative and Goal has a <strong>Score</strong> associated.</p>
  
  <p>Other documentation (listed below) has explained simpler modelling challenges, so I'm still lost. How do I <strong>model the decision matrix</strong> and <strong>use Score attributes</strong>.</p>
  
  <p>Below are code snippets of each model and a test I tried.</p>
  
  <p>Decisions</p>
  
  <pre><code>class Decision &lt; ActiveRecord::Base
    has_many :alternatives, dependent: :destroy
    has_many :goals, dependent: :destroy
    has_many :scores, dependent: :destroy
    validates :name, presence: true, length: { maximum: 50 }
  end
  </code></pre>
  
  <p>Alternatives</p>
  
  <pre><code>class Alternative &lt; ActiveRecord::Base
    belongs_to :decision
    has_many :scores, dependent: :destroy
    validates :decision_id, presence: true
    validates :name, presence: true, length: { maximum: 50 }
  end
  </code></pre>
  
  <p>Goals</p>
  
  <pre><code>class Goal &lt; ActiveRecord::Base
  
    belongs_to :decision
    has_many :scores, dependent: :destroy
    validates :decision_id, presence: true
    validates :name, presence: true, length: { maximum: 50 }
    validates :constraint, inclusion: [true, false]
    validates :rank, numericality: {only_integer: true,
                                      greater_than_or_equal_to: 1},
                                      allow_blank: true
    validates :weight, numericality: {greater_than_or_equal_to: 0,
                                      less_than_or_equal_to: 1},
                                      allow_blank: true
  end
  </code></pre>
  
  <p>Score</p>
  
  <pre><code>class Score &lt; ActiveRecord::Base
    belongs_to :decision
    belongs_to :goal
    belongs_to :alternative
    validates :decision_id, presence: true
    validates :goal_id, presence: true
    validates :alternative_id, presence: true
    validates :rating, numericality: {only_integer: true,
                                      greater_than_or_equal_to: -2,
                                      less_than_or_equal_to: 2},
                                      allow_blank: true
  end
  </code></pre>
  
  <p>I tried the following test in decision_test.rb that doesn't work, before realizing how difficult using Score attributes would be.</p>
  
  <pre><code>test "associated decision data should be destroyed" do
      @decision.save
      @alternative_1 = @decision.alternatives.create!(name: "toaster")
      @goal_1 = @decision.goals.create!(name: "fast")
      @score_1 = @decision.scores.build(
                      params[:score].merge(:alternative_id =&gt; @alternative_1.id,
                                           :goal_id =&gt; @goal_1.id)) ## doesn't work
      assert_difference ['Alternative.count','Goal.count'], -1 do
          @decision.destroy
      end
    end
  </code></pre>
  
  <p>Schema.rb</p>
  
  <pre><code>ActiveRecord::Schema.define(version: 20150816211809) do
  
    create_table "alternatives", force: :cascade do |t|
      t.string   "name"
      t.integer  "decision_id"
      t.datetime "created_at",  null: false
      t.datetime "updated_at",  null: false
      t.decimal  "score"
    end
  
    add_index "alternatives", ["decision_id"], name: "index_alternatives_on_decision_id"
  
    create_table "decisions", force: :cascade do |t|
      t.string   "name"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end
  
    create_table "goals", force: :cascade do |t|
      t.string   "name"
      t.boolean  "constraint",  default: false
      t.integer  "rank"
      t.decimal  "weight"
      t.integer  "decision_id"
      t.datetime "created_at",                  null: false
      t.datetime "updated_at",                  null: false
    end
  
    add_index "goals", ["decision_id"], name: "index_goals_on_decision_id"
  
    create_table "scores", force: :cascade do |t|
      t.integer  "rating"
      t.decimal  "value"
      t.integer  "decision_id"
      t.integer  "goal_id"
      t.integer  "alternative_id"
      t.datetime "created_at",     null: false
      t.datetime "updated_at",     null: false
    end
  
    add_index "scores", ["alternative_id"], name: "index_scores_on_alternative_id"
    add_index "scores", ["decision_id"], name: "index_scores_on_decision_id"
    add_index "scores", ["goal_id"], name: "index_scores_on_goal_id"
  
  end
  </code></pre>
  
  <p>Resources (the most relevant ones):</p>
  
  <ul>
  <li><a href="http://guides.rubyonrails.org/association_basics.html" rel="nofollow noreferrer">http://guides.rubyonrails.org/association_basics.html</a></li>
  <li><a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html" rel="nofollow noreferrer">http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html</a></li>
  <li><a href="https://stackoverflow.com/questions/3911890/rails-belongs-to-many-models">Rails belongs_to many models</a></li>
  <li><a href="https://stackoverflow.com/questions/3401504/can-model-belong-to-two-other-models-and-have-a-nested-relationship">can model &quot;belong_to&quot; two other models and have a nested relationship?</a></li>
  <li><a href="https://stackoverflow.com/questions/12162570/rails-new-entry-to-a-model-that-belongs-to-two-other">Rails: New entry to a model that belongs_to two other</a></li>
  </ul>