2013-12-16 54 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