2012-11-11 73 views
0

我寫的,而不是查詢數據庫三個時間帶開關的情況下一個循環,實例變量,我得到一個查詢中的所有查詢和有條件的循環分配給不同的實例變量(我將以簡單形式用作集合)。我不知道如何將活動記錄關係追加到實例變量,請幫助我。如何關係對象分配到軌

class QuestionsController < ApplicationController 

def index 
    @question = Question.new 
    @question_lookups = Lookup.where({:look_up_for => "question"}) 

    @question_lookups.each do |lk| 
     case lk.look_up_type 
      when 'mode' 
       @question_mode = lk #How can i do this here..? 
      when 'status' 
       @question_status = lk 
      else 
       @question_type = lk 
     end 
    end 
    session[:lk] = @question_mode 

    # @question_mode = Lookup.where({:look_up_for => "question", :look_up_type => "mode"}) 
    # @question_status = Lookup.where({:look_up_for => "question", :look_up_type => "status"}) 
    # @question_type = Lookup.where({:look_up_for => "question", :look_up_type => "type"}) 
end 

end 

回答

1

轉換question_mode等成陣列和推LK到它

@question_mode, @question_status, @question_type = [] 
@question_lookups.each do |lk| 
    case lk.look_up_type 
     when 'mode' 
      @question_mode << lk #How can i do this here..? 
     when 'status' 
      @question_status << lk 
     else 
      @question_type << lk 
    end 
end 
+0

聲明應該逐個像這樣: ' @question_status = [] @question_mode = [] @ question_type = [] ' – Senthil

+0

你的意思是「應該是」是什麼意思?在代碼風格方面?像這樣寫這是一個常見的Ruby範例... – awenkhh