2011-04-01 59 views
0

我有scaffold有2場name:string, active:boolean..將數據插入2和表1個控制器

這樣支架創建數據...

def create 
    @question = Question.new(params[:question]) 
    render :json => @question.to_ext_json(:success => @question.save) 
    end 

一切都很好,但我想,當產生問題的一些數據插入到我的另一個表稱爲標記:

在我的符號表中只有2場:令牌& IS_ACTIVE(布爾)

def create 
    @w = "token" 
    @question = Question.new(params[:question]) 
    @token = Token.new({:token => @w, :is_active=>"1"}) 
    render :json => @question.to_ext_json(:success => @question.save) 
    end 

這種方式不起作用。我怎麼能做到這一點?

回答

0

喜歡的東西...

def create 
    @w = "token" 
    @question = Question.new(params[:question]) 

    if success = @question.save 
    @token = Token.new({:token => @w, :is_active=>"1"}) 
    @token.save 
    end 

    render :json => @question.to_ext_json(:success => success) 
end 

這是假設任何驗證有關問題的模型來完成。你可以更進一步並使用交易,但在這種情況下似乎不必要的複雜。