How to make a Rails model read-only except for specified attributes?

╄→尐↘猪︶ㄣ 提交于 2019-12-13 03:27:01

问题


I have a rails active record model Application and need to make it read-only.

I know that having a before_validation method with:

if locked

  def readonly?
    true
  end

end

Will allow me to make it readonly.

The only issue is that, when I need to disable readonly mode, I can not change the locked value to false because the model is already read-only.

I know that attr_readonly :name, :email, :price etc. will allow me to make specific attributes readonly, I am looking for the inverse of this to only allow specific attributes.

Is there a way to make this read-only method except specific attributes?

来源:https://stackoverflow.com/questions/53432372/how-to-make-a-rails-model-read-only-except-for-specified-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!