Skip to content

Commit 1f14a12

Browse files
committed
Add Read-Only Models as a rails til
1 parent e3a43d2 commit 1f14a12

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
77
warrant a full blog post. These are mostly things I learn by pairing with
88
smart people at [Hashrocket](http://hashrocket.com/).
99

10-
_431 TILs and counting..._
10+
_432 TILs and counting..._
1111

1212
---
1313

@@ -278,6 +278,7 @@ _431 TILs and counting..._
278278
- [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md)
279279
- [Perform SQL Explain With ActiveRecord](rails/perform-sql-explain-with-activerecord.md)
280280
- [Pretend Generations](rails/pretend-generations.md)
281+
- [Read-Only Models](rails/read-only-models.md)
281282
- [Rescue From](rails/rescue-from.md)
282283
- [Retrieve An Object If It Exists](rails/retrieve-an-object-if-it-exists.md)
283284
- [Select A Select By Selector](rails/select-a-select-by-selector.md)

rails/read-only-models.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Read-Only Models
2+
3+
Are you in the midst of a big refactoring that is phasing out an
4+
`ActiveRecord` model? You may not be ready to wipe it from the project, but
5+
you don't want it accidentally used to create any database records. You
6+
essentially want your model to be read-only until it is time to actually
7+
delete it.
8+
9+
This can be achieved by adding a `readonly?` method to that model that
10+
always returns `true`.
11+
12+
```ruby
13+
def readonly?
14+
true
15+
end
16+
```
17+
18+
`ActiveRecord`'s underlying persistence methods always check `readonly?`
19+
before creating or updating any records.
20+
21+
[source](http://stackoverflow.com/questions/5641410/is-there-an-easy-way-to-make-a-rails-activerecord-model-read-only)
22+
23+
h/t Josh Davey

0 commit comments

Comments
 (0)