2013-05-20 160 views
1

我有一個提交模型訪問父對象

belongs_to :report 
has_many :answers 

報表模型

has_many :questions 

問題型號

has_many :answers # answer has submission_id 

現在Rabl的模板,我需要這樣的

輸出
 object @submission 
     attributes :status, :submission_time 

     child :report do 
      attribute :id, :name 
      child :questions do 
       attribute :id, :content 
       node(:answers_count) do |question| 
        question.answers.where("submission_id = ?", @submission.id).count 
       end 
       node(:answers) do |question| 
        question.answers.where("submission_id = ?", @submission.id).collect{ |answer| {:name => answer.name, :id => answer.id}} 
       end 
      end 
     end 

這工作完全正常,但問題是在answers_count和答案節點我使用@submission,因爲該索引操作不起作用。

它很好地顯示操作,但索引操作正在創建問題,因爲@submission在索引操作中不可用。

PS:一旦我解決了這個問題,我會將大查詢從視圖移到模型。我知道,爲了簡單起見,我已經粘貼了內聯。

回答

2

這是不是最好的解決方案,但它會工作

object @submission 
attributes :status, :submission_time 

child :report do 
    attribute :id, :name 

    submission_id = locals['submission'] ? locals['submission'].id : locals[:object].id 

    child :questions do 
     attribute :id, :content 
     node(:answers_count) do |question| 
      question.answers.where("submission_id = ?", submission_id).count 
     end 
     node(:answers) do |question| 
      question.answers.where("submission_id = ?", submission_id).collect{ |answer| {:name => answer.name, :id => answer.id}} 
     end 

    end 
end 
0

這應該適合你,雖然這是一個黑客承認。我已經把下面的非工作的解決方案,爲後人:

object @submission 
attributes :status, :submission_time 

child :report do 
    submission_id = root_object.id 
    attribute :id, :name 
    child :questions do 
     attribute :id, :content 
     node(:answers_count) do |question| 
      question.answers.where("submission_id = ?", submission_id).count 
     end 
     node(:answers) do |question| 
      question.answers.where("submission_id = ?", submission_id).collect{ |answer| {:name => answer.name, :id => answer.id}} 
     end 
    end 
end  

不正確的解決方案:

使用root_object此:

node(:answers_count) do |question| 
    question.answers.where("submission_id = ?", root_object.id).count 
end 

或者,把它作爲參數傳遞給child :report block:

object @submission 
    attributes :status, :submission_time 

    child :report do |submission| 
     attribute :id, :name 
     child :questions do 
      attribute :id, :content 
      node(:answers_count) do |question| 
       question.answers.where("submission_id = ?", submission.id).count 
      end 
      node(:answers) do |question| 
       question.answers.where("submission_id = ?", submission.id).collect{ |answer| {:name => answer.name, :id => answer.id}} 
      end 
     end 
    end 

不幸的是,這些方法都沒有奏效。第一個解決方案不起作用,因爲root_object隨塊範圍而變化(它標識包含塊的父項)。第二個解決方案不起作用,因爲與node不同,child傳遞該迭代的子對象。不幸的是,這與the documentation會讓你相信。

+0

在第一個解決方案root_object指向爲循環和第二個解決方案提交開始指向報表對象的問題對象。 :( –

+0

@MohitJain:你是對的,但是[文檔](https://github.com/nesquena/rabl#child-nodes)中必須有一個錯誤,這似乎表明第二個解決方案會起作用。另外,看到http://stackoverflow.com/questions/15878381/how-to-add-parent-attributes-inside-its-children-in-rabl-template-when-parent-i這似乎有一個類似的問題坦率地說,我很驚訝,RABL使得它很難通過孩子訪問根(相對於一個節點)。 – PinnyM

+0

更新了一個似乎工作的解決方案(有點駭人)。 – PinnyM