Is there a whitelist-like reverse attr_readonly?

attr_protected allows you to mark certain columns as protected, as well as attr_accessible, which allows you to mark all but this set as protected. attr_readonly allows you to mark specific columns as readonly. Is there a converse that allows all columns except the given set to be marked as read-only?

+3
source share
2 answers

Perhaps you are after attr_accessible, which indicates columns that are "assignable by mass" (as in, they can be passed in createand update_attributes). In fact, this would mean that they are write protected.

To set this attribute:

@user.admin = true
@user.save
+1

, , :

after_initialize do
  mutable_attributes = ["status", "updated_at"]
  self.class.attr_readonly *(self.attributes.keys - mutable_attributes)
end
0

Source: https://habr.com/ru/post/1792664/


All Articles