我建模決策矩陣,因此每個決策(其中N),有x個替代可供選擇和y 目標見面。替代方案和目標的每個x * y配對都有得分相關聯。如何訪問所有3個模型都屬於另一個模型的belongs_to 2個模型?
其他文檔(下面列出)解釋了更簡單的建模挑戰,所以我仍然迷失。我如何模型決策矩陣和使用分數屬性。
下面是每個模型的代碼片段和我試過的一個測試。
決定
class Decision < 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
替代
class Alternative < ActiveRecord::Base
belongs_to :decision
has_many :scores, dependent: :destroy
validates :decision_id, presence: true
validates :name, presence: true, length: { maximum: 50 }
end
目標
class Goal < 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
分數
class Score < 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
在我意識到使用Score屬性有多困難之前,我在decision_test.rb中嘗試了下列不起作用的測試。
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 => @alternative_1.id,
:goal_id => @goal_1.id)) ## doesn't work
assert_difference ['Alternative.count','Goal.count'], -1 do
@decision.destroy
end
end
Schema.rb
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
資源(最相關的):
- http://guides.rubyonrails.org/association_basics.html
- http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
- Rails belongs_to many models
- can model "belong_to" two other models and have a nested relationship?
- Rails: New entry to a model that belongs_to two other
你不介意加入你的問題「schema.rb」創建腳本表「算賬」? –
看起來你可能想要在這裏使用多態關係,更新的'accep_nested_attributes'形式,或者涉及'has_many ...:through'類型的語法。我們確實需要查看您的模式以瞭解您所得到的結果,因爲我懷疑您沒有以這種方式構建好數據結構。 – Kelseydh
謝謝@TheFabio。我已經在上面添加了它。 – purplengineer