Skip to content

Commit 1de308f

Browse files
committed
Add Add ActiveRecord Error Not Tied To Any Attribute as a Rails til
1 parent 6282b4b commit 1de308f

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1212

13-
_1131 TILs and counting..._
13+
_1132 TILs and counting..._
1414

1515
---
1616

@@ -650,6 +650,7 @@ _1131 TILs and counting..._
650650
- [Add A Check Constraint To A Table](rails/add-a-check-constraint-to-a-table.md)
651651
- [Add A Foreign Key Reference To A Table](rails/add-a-foreign-key-reference-to-a-table.md)
652652
- [Add A Reference Column With An Index](rails/add-a-reference-column-with-an-index.md)
653+
- [Add ActiveRecord Error Not Tied To Any Attribute](rails/add-activerecord-error-not-tied-to-any-attribute.md)
653654
- [Add React With Webpacker To A New Rails App](rails/add-react-with-webpacker-to-a-new-rails-app.md)
654655
- [Add timestamptz Columns With The Migration DSL](rails/add-timestamptz-columns-with-the-migration-dsl.md)
655656
- [Access Secrets In A Rails 5.2 App](rails/access-secrets-in-a-rails-5-2-app.md)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Add ActiveRecord Error Not Tied To Any Attribute
2+
3+
Often the [errors on an ActiveRecord
4+
object](https://api.rubyonrails.org/v6.1.3.2/classes/ActiveModel/Errors.html)
5+
are tied to a specific attribute of that object. For instance, when this
6+
validation is violated
7+
8+
```ruby
9+
validates :name, presence: true
10+
```
11+
12+
Then the error will be tied to `:name`.
13+
14+
With the
15+
[`ActiveModel::Errors#add`](https://api.rubyonrails.org/v6.1.3.2/classes/ActiveModel/Errors.html#method-i-add)
16+
method, we can write custom validation logic that ties an error to a specific
17+
attribute.
18+
19+
```ruby
20+
validate :quantity_for_bulk_purchase
21+
22+
def quantity_for_bulk_purchase
23+
return if purchase_type != :bulk
24+
25+
if quantity < 12
26+
errors.add(:quantity, "must be greater than 12 for bulk purchases")
27+
end
28+
end
29+
```
30+
31+
Errors don't have to be tied to specific attribute. They can be tied to the
32+
object as a whole. This can be better for validations, like the one above, that
33+
involve multiple attributes.
34+
35+
```ruby
36+
validate :quantity_for_bulk_purchase
37+
38+
def quantity_for_bulk_purchase
39+
return if purchase_type != :bulk
40+
41+
if quantity < 12
42+
errors.add(:base, "Quantity must be greater than 12 for bulk purchases")
43+
end
44+
end
45+
```
46+
47+
By using the `:base` symbol, we are ascribing this error to the object as a
48+
whole.
49+
50+
```
51+
> my_object.errors
52+
#=> #<ActiveModel::Errors:0x00007fccaa5a8740
53+
@base=
54+
#<MyObject:0x00007fcc8a5e9238
55+
...
56+
@details={:base=>[{:error=>"Quantity must be greater than 12 for bulk purchases"}]},
57+
@messages={:base=>["Quantity must be greater than 12 for bulk purchases"]}>
58+
59+
> my_object.errors.full_messages
60+
#=> ["Quantity must be greater than 12 for bulk purchases"]
61+
```

0 commit comments

Comments
 (0)