允许用户指定哪些属性可供公众使用的最佳方式?
在我的一张表(卖家)中,所有字段都需要用户的许可才能供其他人(注册或未注册)查看。我可以遍历并为每个列创建一个关联的列,例如。 (列 1、列 1_隐私、列 2、列 2_隐私)。然而,这似乎是多余且糟糕的设计。
Rails 中有没有更优雅的解决方案?提前致谢。
In one of my tables (sellers), all the fields requires permission from the user for others (registered or non-registered) to see. I could go through and create an associated column for each column, ex. (column1, column1_privacy, column2, column2_privacy). However, this seems redundant and bad design.
Is there a more elegant solution for this in Rails? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会称其为多余或糟糕的设计。如果您想要每列访问控制,那么在某个地方您将需要每列一个标志;您可以将所有这些标志打包到一个序列化结构中,但这样您就会与关系数据库作斗争:
您可以使用包含列名称的单独 EAV 样式表,但在添加和删除列时仍然需要清理它。使用 EAV 表进行查询往往很快就会变得一团糟。
每个访问控制列都有一个标志列,易于设置、易于查询、易于编辑、易于自动构建表单,并且可以轻松避免数据库混乱。额外的列可能会让你的桌子有点宽,但我不会太担心。
额外的列可以轻松地从模型中提取可显示的信息:
然后您可以在控制器中这样说:
并且您的视图不必过多担心访问控制,它们只需显示哈希中的内容即可。当然,这个技巧可以与其他实现一起使用,但如果您使用“隐私栏”想法,那就非常简单了。
I wouldn't call it redundant or bad design. If you want per-column access control then somewhere you're going to need one flag per column; you can pack all those flags into one serialized structure but then you'd be fighting against your relational database:
You could use a separate EAV-style table that contains column names but you'd still have to clean it as you add and remove columns. And querying with an EAV table tends to turn into an ugly mess quite quickly.
One flag-column for each access controlled column is easy to set up, easy to query, easy to edit, easy to automatically build forms for, and makes it easy to not make a mess in your database. The extra columns may make your tables a little on the wide side but I wouldn't worry about that too much.
The extra columns make it easy to extract the displayable information from your model:
Then you can say things like this in your controller:
and your views don't have to worry too much about the access control, they can just show what's in the Hash. This trick can, of course, be used with the other implementations but it is dead easy if you use your "privacy column" idea.
您可以创建一个名为
SharedColumn
的关联模型Seller:
SharedColumn
然后您可以使用
column_names
方法来迭代复选框列表,例如此控制器
视图
You could create a associated model named
SharedColumn
Seller:
SharedColumn
Then you may use the
column_names
method for iteration of a list of checkboxes like thisController
View