2012-09-11 75 views
0

我正在使用ActiveAdmin。我需要存儲創建或更新每條記錄的用戶。在Rails3中獲取模型中的當前用戶

因此,我將user_created_id和user_updated_id添加到每個模型。 在before_save上,我想存儲有關current_admin_user的信息。問題是我無法訪問模型中的current_admin_user。

有沒有辦法做到這一點,而不打破MVC?

我發現這個:http://rails-bestpractices.com/posts/47-fetch-current-user-in-models 但我不確定它是否安全。

任何幫助?

回答

0

做到在控制器(它是控制器存在的理由)保存前:

def create 
    @my_model = MyModel.create params[:my_model] 
    @my_model.user_created = current_user 
    @my_model.save 
end 

同樣與update方法。

+0

我正在使用ActiveAdmin,這意味着我正在使用ActiveAdmin控制器。 – Tony