Ruby on Rails is a great framework and with Active Record, its uses are almost endless. By using two or three lines of command, you can create a complete MVC program that amounts for many different uses. You can build everything from a blog platform to a dog party planning website (which if you do, please share it with me).
The only issue would be controlling your client-side. We all know clients are wild, and the internet is full of trolls. How do we make sure our program is being used for its full purpose? Or at least receiving the right information?
Validation comes in hand for that. Validations are used to ensure that only valid data is saved into your database. They are database agnostic, cannot be bypassed by end users, and are convenient to test and maintain.
You can be very specific in your validations, provided that rails give us a generous amount of validation helpers. The most common ones are acceptance, length, presence, numericality and uniqueness. In this post we are gonna be showing how to use those examples, but don't restrain yourself!
Example: blog plataform
Imagine you're building a short blog platform (like Twitter), and you need to make sure every user is following the rules for blog posts, tags and title. This is what our post model looks like now:
post table
class Post < ApplicationRecord
belongs_to :user
end
Pretty empty, but we will change that soon. We add all of our validations to our model componente. We want to make sure that every post has a content, at least one tag and a title. Let's validate the presence of those:
post table
class Post < ApplicationRecord
belongs_to :user
validates :content, :tags:, :title, presence:true
end
validates :element is our syntax for validations. Adding presence validates that the specified attributes are not empty.
Great, now we can't create a post without a content, a tag and a title. But we also don't want blog posts or titles that are just a single word. Let's add a Length helper to that:
post table
class Post < ApplicationRecord
belongs_to :user
validates :content, :tags:, :title, presence:true
validates :content, :title, length {in: 20..}
end
This helper validates the length of the attributes' values. Length also has many attributes that can be use to constrain the element in different ways (ex: {minimum:},{maximum:},{in: (range)}, {is:}.
While adding tags during our test, we see that the same tag can be add an infinity number of times. That's not right! We want to make sure that every tag can only be added one time into each post. Let's use our uniqueness helper for that.
post table
class Post < ApplicationRecord
belongs_to :user
validates :content, :tags:, :title, presence:true
validates :content, :title, length {in: 20..}
validates :tags, uniqueness: true
end
Validations help us control the information sent and received to our database. Along with error messages, they're also very useful to guide the user with application usage. You can explore more of it with the rails documentation, which is very extend. I hope this post helped you with it, and happy coding!!!!
Top comments (0)