简体   繁体   English

Rails 3设计:“隐藏”具有“属性”为true的条目,但仅在api级别

[英]Rails 3 design: “hiding” an entry with “attribute” true, but only on the api level

In a rails 3 app, I have a model with a boolean attribute called "archived". 在Rails 3应用中,我有一个带有布尔属性的模型,称为“存档”。 This attribute is accessible in the cms, where I can go in and edit individual objects, and a checkbox where I can check if an object is archived or not. 该属性可在cms(我可以进入和编辑单个对象)以及一个复选框(在其中可以检查对象是否已存档)中访问。 However, if this object is archived, I don't want this object to be accessible on the api level. 但是,如果此对象已存档,则我不希望在api级别上访问此对象。 That includes the object itself, and its associated objects. 这包括对象本身及其关联的对象。 At the same time I would like the object to be accessible in the cms, to edit other details and set the archived value back to false, so that I can access the object again in the api and change its behaviour back to default. 同时,我希望可以在cms中访问该对象,以编辑其他详细信息并将存档值设置为false,以便可以再次在api中访问该对象并将其行为更改为默认值。

I would really appreciate suggestions on how to approach this from a design/high level perspective. 我非常感谢关于从设计/高级角度解决此问题的建议。 Thank you for your help. 谢谢您的帮助。

When you say "api" do you mean: 当您说“ api”时,您的意思是:

  1. Public RESTful API 公共RESTful API
  2. Access to those models in your own code. 用您自己的代码访问这些模型。 Like finder methods on your model? 像模型上的查找器方法一样?

For the first case, simply get only objects in required state ( Model.where(archived: false) ). 对于第一种情况,只需仅获取处于所需状态的对象( Model.where(archived: false) )。

In the second case, taking into account that Ruby is very "open" language, it would be impossible to limit access to any of object property or method. 在第二种情况下,考虑到Ruby是非常“开放”的语言,将不可能限制对任何对象属性或方法的访问。 However, to build a public API for other team members to use, you can add methods that correctly filter out your models (look into scope ). 但是,要构建供其他团队成员使用的公共API,您可以添加正确过滤掉模型的方法(查看scope )。

As a second alternative to Model.where(archived: false) 作为Model.where(archived:false)的第二种选择

If you used a status enumerator instead of a boolean, you would be able to restrict the list using Model.archived, or Model.published... All you need is an integer field for the model's db table, and something like this in the model: 如果您使用状态枚举器而不是布尔值,则可以使用Model.archived或Model.published来限制列表。您所需要的只是模型的db表的整数字段,并且在模型:

  enum status: { dev:0, published: 1, archived: 2}

Then in your API, you define what status segment you want to restrict it to as shown above. 然后在您的API中,定义要将状态限制为哪个状态段,如上所示。 The enumerator system in RoR is a little tricky to get the hang of at first, but very powerful once you get it. RoR中的枚举器系统刚开始时有点棘手,但是一旦掌握,它就会非常强大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM