Rails 5.1 Otomatis Validasi Presence Pada Belongs To Association
- Last modified atPada Rails 5.1 saat suatu model memiliki assosiasi belongs to otomatis diberikan validasi presence. Belum dicoba pada Rails 5.0, tetapi setahu saya pada Rails 4 validasi ini tidak ada.
# app/models/article.rb
class Comment < ApplicationRecord
belongs_to :article
end
comment = Comment.new
comment.valid? # => false
comment.errors.messages # => {:article=>["must exist"]}
Sebelum Rails 5.1, diperlukan tambahan validasi seperti dibawah ini.
# app/models/article.rb
class Comment < ApplicationRecord
belongs_to :article
# tambahan validasi seperti ini
validates :article, presence: true
# atau
validates :article_id, presence: true
end
Recent Posts
AWS Secrets Manager
Explanation about AWS Secrets Manager with example code.
Envelope Encryption
Envelope encryption is the practice of encrypting plaintext data with a data key, and then encrypting the data key under another key.
Tutorial - "su username vs su - username" - A Security Perspective
The main difference between `su username` and `su - username` lies in the environment variables that are loaded when switching to the specified user.
Subdomain Hijacking
My dormant subdomain was recently hijacked, redirecting it to a online gamble registration page.
C# DbContext ServiceLifeTime
my note about C Sharp ServiceLifeTime