2013-12-16 52 views
0

我有一个可以代表参加的调查显示:的has_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 
+0

请张贴您的模型与关系定义。 – vee

+0

这个人的模型可能会给你的一些亮光http://stackoverflow.com/questions/20612855/redirecting-to-new-page-with-already-existing-records-after-user-login-for-authe –

回答

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需要很多Surveys,Survey有很多Questions,Question有很多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不应该是复数。

第二个问题是你有Userhas_and_belongs_to_manySurveys。要定义many to many关系Survey之间User,所以你需要定义同在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 

使用上述模型relationshp你可以建立你的SurveysControllernewcreate行动将类似于如下:

# 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定义many to many关系。

+0

谢谢为你的伟大解释和分享你的想法。我明确地可以部分地实现你的代码“块”。在我看来,我必须使用参与者模型。背景中还有更多的逻辑(用户可以参加调查并向参与者发放随机代码,让他们匿名参加。参与者没有权利,用户应该支付等等......我想我不能排除参与者。 – Jan