我建模决策矩阵,因此每个决策(其中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