我有参与者可以参加的调查:
调查has_many:has_many:答案
的问题参与者应该能够将他的投票添加到每个答案中(值为[-1,0,1])
一个问题如下:
Question 1
- Answer1 -> Please vote -1 / 0 / 1 (Checkbox field)
- Answer2 -> Please vote -1 / 0 / 1 (Checkbox field)
- Answer3 -> Please vote -1 / 0 / 1 (Checkbox field)
- Participant Comment (Text Field)
- Participant Average (Rating Field)
如何正确保存投票?我想保持participant_id, answer_id AND voting_value
得到保存。
增加了解释:
(用户将得到答案)
User < ActiveRecord::Base
has_and_belongs_to_many :surveys
has_many :participants
end
Survey < ActiveRecord::Base
belongs_to :user
has_many :questions
end
Question < ActiveRecord::Base
belongs_to :survey
has_many :answers
end
Answers < ActiveRecord::Base
belongs_to :question
has_many :votings
end
Voting < ActiveRecord::Base
belongs_to :answer
belongs_to :participant
attr_accessible :value
end
Participant < ActiveRecord::Base
has_many :votings
belongs_to :user
end
答案 0 :(得分:0)
我认为您的participant
和user
模型需要合并为一个,因为不是participant
和user
?我不明白为什么Participant
模型在这里是必要的。
说,我们将User
和Participant
合并到User
,User
和Voting
之间的关联将是:
User < ActiveRecord::Base
has_and_belongs_to_many :surveys
has_many :votings
end
Voting < ActiveRecord::Base
belongs_to :answer
belongs_to :user
attr_accessible :value
end
现在从你的第一个代码块开始:
Question 1
- Answer1 -> Please vote -1 / 0 / 1 (Checkbox field)
- Answer2 -> Please vote -1 / 0 / 1 (Checkbox field)
- Answer3 -> Please vote -1 / 0 / 1 (Checkbox field)
- Participant Comment (Text Field)
- Participant Average (Rating Field)
用户的答案是否未完成投票?在那种情况下,我们需要user has_many :votings
关系吗?我认为发出的关联是User
需要很多Surveys
,Survey
有很多Questions
,Question
有很多Answers
而答案有很多Votings
。可以检查每个投票。因此,我不认为User
和Voting
之间的直接关联是解决方案。
以下内容已更新User
,Answer
和Voting
型号,并附有更新关联:
User < ActiveRecord::Base
has_and_belongs_to_many :surveys
end
Answer < ActiveRecord::Base
belongs_to :question
has_many :votings
end
Voting < ActiveRecord::Base
belongs_to :answer
attr_accessible :value
end
最后还有一些其他小错别字错误,例如Answers < ActiveRecord::Base
,其中班级Answer
不应该是复数。
第二个问题是你有User
has_and_belongs_to_many
Surveys
。您正在定义many to many
和User
之间的Survey
关系,因此您需要在Survey
模型中定义相同的内容:
Survey < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :questions
end
通过所有这些更新,您的模型以及关系将如下所示(注意:我还在模型中添加了accepts_nested_attributes_for
,以便在您提交表单时无需手动构建每个关联对象):
User < ActiveRecord::Base
has_and_belongs_to_many :surveys
end
Survey < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :questions
accepts_nested_attributes_for :questions
end
Question < ActiveRecord::Base
belongs_to :survey
has_many :answers
accepts_nested_attributes_for :answers
end
Answers < ActiveRecord::Base
belongs_to :question
has_many :votings
accepts_nested_attributes_for :votings
end
Voting < ActiveRecord::Base
belongs_to :answer
attr_accessible :value
end
使用上述模型关系,您可以构建SurveysController
的{{1}}和new
操作看起来类似于以下内容:
create
另外,我建议您调查# app/controllers/surveys_controller.rb
class SurveysController < ApplicationController
def new
@survey = current_user.surveys.build do
@question = @survey.questions.build
@answer = @question.answers.build
# Build three different votings for answer with default value.
[-1, 0, 1].each do |voting_value|
@answer.votings.build(value: voting_value)
end
end
def create
@survey = current_user.surveys.build(params[:survey])
@survey.save
# Of course you would check if save succeeded or failed and take action accordingly.
end
end
而不是has_many...through
来定义has_and_belongs_to_many
关系。