1
0
mirror of https://github.com/jbranchaud/til synced 2026-07-09 02:20:33 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
nick-w-nick 32536e9303 Merge 295fe153ad into fc93ecfed4 2024-09-30 23:23:11 -04:00
jbranchaud fc93ecfed4 Add Validate Column Data With Check Constraints as a Rails TIL 2024-09-30 19:50:52 -05:00
nick-w-nick 295fe153ad added mention of ES6 compatibility
Hello, I've added a small blockquote below the description to indicate that this method of accessing an indefinite number of function arguments has been superseded by the use of the spread operator via rest parameters for ES6+ compatibility.
2022-01-06 11:39:04 -05:00
3 changed files with 36 additions and 1 deletions
+2 -1
View File
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1447 TILs and counting..._ _1448 TILs and counting..._
--- ---
@@ -986,6 +986,7 @@ _1447 TILs and counting..._
- [Update Column Versus Update Attribute](rails/update-column-versus-update-attribute.md) - [Update Column Versus Update Attribute](rails/update-column-versus-update-attribute.md)
- [Upgrading Your Manifest For Sprocket's 4](rails/upgrading-your-manifest-for-sprockets-4.md) - [Upgrading Your Manifest For Sprocket's 4](rails/upgrading-your-manifest-for-sprockets-4.md)
- [Use IRB And Ruby Flags With Rails Console](rails/use-irb-and-ruby-flags-with-rails-console.md) - [Use IRB And Ruby Flags With Rails Console](rails/use-irb-and-ruby-flags-with-rails-console.md)
- [Validate Column Data With Check Constraints](rails/validate-column-data-with-check-constraints.md)
- [Verify And Read A Signed Cookie Value](rails/verify-and-read-a-signed-cookie-value.md) - [Verify And Read A Signed Cookie Value](rails/verify-and-read-a-signed-cookie-value.md)
- [Where Am I In The Partial Iteration?](rails/where-am-i-in-the-partial-iteration.md) - [Where Am I In The Partial Iteration?](rails/where-am-i-in-the-partial-iteration.md)
- [Why Redirect And Return In Controllers](rails/why-redirect-and-return-in-controllers.md) - [Why Redirect And Return In Controllers](rails/why-redirect-and-return-in-controllers.md)
@@ -5,6 +5,8 @@ an array-like object with all of the arguments to the function. Even if not
all of the arguments are referenced in the function signature, they can all of the arguments are referenced in the function signature, they can
still be accessed via the `arguments` object. still be accessed via the `arguments` object.
> For ES6+ compatibility, the `spread` operator used via [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) is preferred over the `arugments` object when accessing an abritrary number of function arguments.
```javascript ```javascript
function argTest(one) { function argTest(one) {
console.log(one); console.log(one);
@@ -0,0 +1,32 @@
# Validate Column Data With Check Constraints
A check constraint is a feature of database systems like PostgreSQL that allows
you to enforce rules about the data that goes in a table's column. As of Rails
6.1, ActiveRecord provides a way to add a check constraint via the DSL.
In this example, we want to ensure that the value going into the
reading_statuses.status column is one of four values. Nothing else besides
these four values should be allowed.
```ruby
class AddReadingStatusTable < ActiveRecord::Migration[7.2]
def change
create_table :reading_statuses do |t|
t.references :user, null: false, foreign_key: true
t.references :book, null: false, foreign_key: true
t.string :status, null: false
t.timestamps
end
add_check_constraint
:reading_statuses,
"status in ('started', 'completed', 'abandoned', 'already_read')",
name: "reading_statuses_valid_status_check"
end
end
```
The `#add_check_constraint` method takes the name of the table and a SQL clause
that can evaluate to true or false for a given row. We can optionally include
the name of the check constraint (e.g. {table_name}_{column_name}_check) like
we've done above.