DRYing out Rails Flashcards
Add Validation to this..
Class Person < ApplicationRecord
end
Then call the validation.
Class Person < ApplicationRecord validates :name, presence: true end Person.create(name: "John Doe").valid? #True Person.create(name: nil).valid? #False
Spot the Error class Library < ApplicationRecord has_many :books validates_association :books end
class Book < ApplicationRecord
has_many :libraries
validates_association :libraries
end
The validations create an infinite loop.
Name the Validations you would use for these scenarios:
- A check box being marked
- I am creating a book which is of association library. I want to validate what is in library as well as book when creating a book.
- Two Text Fields should receive the same values, like password and confirm password.
- Validate that certain values are not included in a set.
- Acceptance Validation.
validates :terms_of_service, acceptance :true - Validates Associated
validates_associated :books - Confirmation:
validates :email, confirmation: true - Exclusion
validates :subdomain, exclusion: { in : w%(www}
What is Dry?
Don’t Repeat Yourself
How is validation Dry?
Well we would have to have a validity constraint and that could be put anywhere. SO we DRY it out by putting it in just the Model.
How can I see what didn’t pass validation?
m.errors[:release_date] will show me the errors of release data validation.
What are Lifecycle Callbacks?
Before you talk to the database, you can have these callbacks run. Like you can uppercase some of these things before the value hits the server. There are control on validations before the create. Or maybe an audit record or log entry after the validation. The point cuts are set though.
Can we put validations anywhere?
No we have certain places for validation and we must follow them. We also have to be cautious that we don’t know where the validation comes from
What are constraints on a controller action?
Typical is that you want to only be able to do stuff when you are logged in per say.
Setup a Filter in an application
class applicationController ‘login’
protected
def set_current_user
@current_user=Moviegoer.find_by_id(session[:user_id])
redirect to ‘/login’ and return unless @current_user
end
end
The Redirect terminates the default execution that would happen after the filter. The filter could unexpectedly get triggered though.
What does Protected do in the controller?
Prevents method from being called by any routes.
I have a route that I made in my controller, but it seems to not be working. When I debug I see that my controller method pertaining to that route never gets called. What could be happening?
I am not calling the right route. I am not calling the right redirect.
It is more likely that I have a before_filter somewhere in the code that is preventing the controller method from being called in the first place.
What is the scope a before_filter? of the ApplicationController before_filters?
The Filters declared in a controller also apply to all of its subclasses. That means the filters in ApplicationController apply EVERYWHERE.
Can a filter change the flow of execution?
Yes by calling redirect or render it can prevent the default controller method from a route from ever being called.
Compare Validations and Filters
Both Are Dry
Both are point cuts, but rails gets to determine where these point cuts are.
Only Filters can change execution flow
Filters capture errors in flash, validation captures errors in its errors object.