【发布时间】:2015-08-17 01:28:10
【问题描述】:
我正在建模一个决策矩阵,因此对于每个 Decision(其中 n 个),有 x 个 Alternatives 可供选择,y 个 Goals强>满足。备选方案和目标的每对 x*y 对都有一个关联的分数。
其他文档(如下所列)解释了更简单的建模挑战,所以我仍然迷路了。如何对决策矩阵建模并使用分数属性。
下面是每个模型的代码sn-ps和我试过的一个测试。
决定
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
我在 decision_test.rb 中尝试了以下测试,但没有成功,然后才意识到使用 Score 属性有多么困难。
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
资源(最相关的):
【问题讨论】:
-
您介意在您的问题中添加“scores”表的“schema.rb”创建脚本吗?
-
您似乎想在这里使用多态关系、
accepts_nested_attributes形式的更新或涉及has_many ... :through类型的语法。我们确实需要查看您的架构以了解您的目的,因为我怀疑您没有为此以一种好的方式构建您的数据结构。 -
感谢@TheFabio。我已经在上面添加了。
-
@Kelseydh - 多态关系会保持得分与备选方案和目标的关联吗?
-
@purplengineer 您的目标、分数、决策和备选方案的架构在哪里?我在您的架构中没有看到这些表。
标签: ruby-on-rails matrix associations rails-models