在模型中访问设备的 current_user [英] Access Devise's current_user in Model

查看:17
本文介绍了在模型中访问设备的 current_user的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问模型中的 Devises 'current_user' 对象?我想在他们创建某些内容时自动将当前用户 ID 附加到记录中.我知道我可以在控制器内部手动设置 user_id,但我觉得如果它由模型处理会更容易/更干净.

How do I go about accessing Devises 'current_user' object inside of a model? I want to automatically append the current users id to a record when they create something. I know that I can manually set the user_id inside of the controller but I feel it would be easier/cleaner if it was handled by the model.

推荐答案

本质上,这种逻辑不属于Model,所以最好通过函数传入current_user在模型中或在创建时.这个答案总结了它

Essentially, this kind of logic doesn't belong in a Model, so you're best to pass current_user in through a function in the model or on creation. This answer sums it up.

我知道,我知道,这不是你想要的答案.不幸的是,它在您看来可能更清晰,但它会为您的模型增加您真正不想要的开销.

I know, I know, that's not the answer you wanted. Unfortunately, it may be cleaner looking to you, but it adds overhead to your model that you really don't want.

这篇关于在模型中访问设备的 current_user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆