2010-05-15 36 views
0

我開發了一個應用程序,我似乎遇到了一些與我的關聯有關的問題。我有以下幾點:將兩個變量傳遞給單獨的表...關聯問題

class User < ActiveRecord::Base 
    acts_as_authentic 

    has_many :questions, :dependent => :destroy 
    has_many :sites , :dependent => :destroy 
end 

問題

class Question < ActiveRecord::Base 
    has_many :sites, :dependent => :destroy 
    has_many :notes, :through => :sites 
    belongs_to :user 
end 

網站(認爲這是問題的答案)

class Site < ActiveRecord::Base 
    acts_as_voteable :vote_counter => true 

    belongs_to :question 
    belongs_to :user 
    has_many :notes, :dependent => :destroy 
    has_many :likes, :dependent => :destroy 

    has_attached_file :photo, :styles => { :small => "250x250>" } 

    validates_presence_of :name, :description 

end 

當一個站點(答案)創建我成功通過question_id到Sites表,但我無法弄清楚如何傳遞user_id。這裏是我的SitesController#創建

def create 
     @question = Question.find(params[:question_id]) 
     @site = @question.sites.create!(params[:site]) 

     respond_to do |format| 
      format.html { redirect_to(@question) } 
      format.js 
     end 
     end 

回答

0

我認爲這將做的工作

@question = current_user.questions.find params[:question_id] 

如果不是,那麼就分配mannualy。

@site = @question.sites.build(params[:site]) 
@site.user = current_user 
@site.save 
+0

剛剛嘗試過,仍然成功傳遞了question_id而不是user_id。 – bgadoci 2010-05-15 03:15:23

+0

做到了。謝謝。 – bgadoci 2010-05-15 03:28:52