检索未指定属性的用户对象
这可能非常简单:我有一个控制器,在传递参数时返回用户对象。
def show
if params[:mob]
user = User.find(params[:id])
respond_with([user.establishments,
user])
这将返回整个用户对象。这是一个问题,因为用户对象上有加密的密码、哈希值和我不想公开的各种其他数据。
让它返回用户对象减去一些指定的属性的正确语法是什么?我正在寻找一种解决方案,无需从我想要公开的用户属性中手动创建新的哈希值,因为直接说“给我没有 x 和 y 的用户”比“给我一个哈希值”更简单user.a、user.b、user.c、...user.n”
谢谢!
This might be extremely simple: I have a controller that returns a user object when passed a param.
def show
if params[:mob]
user = User.find(params[:id])
respond_with([user.establishments,
user])
this returns the entire user object. That is a problem because on the user object is the encrypted password, hash, and various other pieces of data I do not want to expose.
What would be the proper syntax to have it return the user object less some specified attributes? I am looking for a solution where I do not have manually create a new hash out of the user attributes I want to expose because it would be must simpler to just say, "give me user without x and y" than "give me a hash of user.a, user.b, user.c, ... user.n"
thx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设当您以 xml 或 json 返回数据时这是一个问题。
您可以通过执行类似的操作来排除某些字段来解决此问题。
或者您可以覆盖模型中的 to_xml 函数以始终排除值。
I'm assuming this is a problem for when you return the data in xml or json.
You can get around this by doing something like this to exclude certain fields.
or you can override the to_xml function in the model to always exclude values.
这是另一个建议。
创建一个“净化”用户的新方法:
Here's a another suggestion.
Create a new method that "sanitizes" the user:
您可以在 User 对象上添加一个实例方法,该方法将返回具有所需属性的新 User 对象吗?
Can you add a instance method on the User object which would return a new User object with the required attributes?