mirror of
https://github.com/jbranchaud/til
synced 2026-01-08 01:28:02 +00:00
Add Read-Only Models as a rails til
This commit is contained in:
23
rails/read-only-models.md
Normal file
23
rails/read-only-models.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Read-Only Models
|
||||
|
||||
Are you in the midst of a big refactoring that is phasing out an
|
||||
`ActiveRecord` model? You may not be ready to wipe it from the project, but
|
||||
you don't want it accidentally used to create any database records. You
|
||||
essentially want your model to be read-only until it is time to actually
|
||||
delete it.
|
||||
|
||||
This can be achieved by adding a `readonly?` method to that model that
|
||||
always returns `true`.
|
||||
|
||||
```ruby
|
||||
def readonly?
|
||||
true
|
||||
end
|
||||
```
|
||||
|
||||
`ActiveRecord`'s underlying persistence methods always check `readonly?`
|
||||
before creating or updating any records.
|
||||
|
||||
[source](http://stackoverflow.com/questions/5641410/is-there-an-easy-way-to-make-a-rails-activerecord-model-read-only)
|
||||
|
||||
h/t Josh Davey
|
||||
Reference in New Issue
Block a user