has_many:通过Survey属性

时间:2013-12-16 15:01:51

标签: ruby-on-rails has-and-belongs-to-many

我有参与者可以参加的调查:

调查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

1 个答案:

答案 0 :(得分:0)

我认为您的participantuser模型需要合并为一个,因为不是participantuser?我不明白为什么Participant模型在这里是必要的。

说,我们将UserParticipant合并到UserUserVoting之间的关联将是:

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需要很多SurveysSurvey有很多QuestionsQuestion有很多Answers而答案有很多Votings。可以检查每个投票。因此,我不认为UserVoting之间的直接关联是解决方案。

以下内容已更新UserAnswerVoting型号,并附有更新关联:

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 manyUser之间的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关系。