Rails 3: how to generate custom error message from failed validation - ruby-on-rails

I'm using
validates :feed_id, presence: true, uniqueness: true
How should I be generating a custom error message to specify that the user has already subscribed to this feed (the feed_id) field is a duplicate
I know I can just do validate_uniqueness_of but it would clutter up the code unnecessarily. How do I pass a specific error message if uniqueness validation fails??

Put a hash with the key message and desired message as the value instead of true:
validates :feed_id, presence: true, uniqueness: {message: "already subscribed"}

Related

validate vs validate_uniquess_of?

Is there a difference between using
validates :foo, uniqueness: true
or
validates_uniqueness_of :foo?
I know this is a simple questions, but Google didn't help
When and why should one be used over the other?
The validates method is a shortcut to all the default validators that Rails provides. So, validates :foo, uniqueness: true would trigger UniquenessValidator under the hood. The source code for validates can be found in the API doc here. As shown there, it basically triggers the validators of the options passed and raises an error in case an invalid option is passed.
validates_uniqueness_of also triggers the UniquenessValidator, the same as validates. Its source code is
# File activerecord/lib/active_record/validations/uniqueness.rb, line 233
def validates_uniqueness_of(*attr_names)
validates_with UniquenessValidator, _merge_attributes(attr_names)
end
The only difference is that with validates_uniqueness_of, we can ONLY validate the uniqueness and not pass additional options, whereas validates accepts multiple options. So we could have the following validations with validates:
validates :name, presence: true, uniqueness: true, <some other options>
But the same would not be possible with validates_uniqueness_of.

Rails validations presence of either one of two fields (XOR)

How can I validate that either one of these values is present, but not both?
validates_presence_of :client_id, message: 'Please enter a value'
validates_presence_of :agency_id, message: 'Please enter a value'
I looked on the rails guides and I think I need to use conditional validations, but I'm still a little stuck.
Try this
validates :client_id, presence: true, unless: :agency_id
validates :agency_id, presence: true, unless: :client_id
If you want to include the error message, you can do
validates :client_id, presence: { message: "Must have a value" }, unless: :agency_id
You can read more about validation messages
If you use the unless syntax, you will get 2 errors: one when client_id and one when agency_id if both are Nil.
You would need a custom method if you want only one error. Guides: ActiveRecord Validation
validate :client_or_agency
def client_or_agency
errors.add(:client_id, "Either Client or Agency needs a value") unless client_id.present? || agency_id.present?
end

rails validations allow_blank and presence

I am reading the book 'agile web development with rails' and I am at the part where they are going through validations, listed below:
class Product < ActiveRecord::Base
validates :description, :title, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\z}i,
message: 'Must include an image file extension'
}
end
Something I am not understanding is that we have image_url, allow_blank set to true, but then we have to verify that the image_url is present? This seems like a contradiction to me at first glance, but I'm sure it's from lack of understanding.
What is it that the allow_blank validation is doing? And why do we not validate the :price be present also?
I can see why you are confused about this---it is not very clear! The meaning of allow_blank: true is that if image_url is blank, then the format validator will not run. The presence validator will still run, because its declaration has no allow_blank option.
The reason the book is doing it this way is to avoid showing users 2 validation messages if they leave the field blank. You don't really want users to see "Image Url can't be blank; Image Url must include an image file extension". It's better to just show a message about it being blank. In other words, we only want to run the format validator if there is something to validate.

Rails Validation with if Condition

I just found the following line of code in a project I'm working on at my new employer:
validates :message, presence: true, if: :message
Am I missing something, or is this pointless?
It seems that it's validating the presence of the message, but only in the case that the message is set.
Validates is uses for Model level validation.
validates :message, presence: true
means you have to insert atleast one character.
If you are entering message on view then it is pointless to use if condition.
i assume this was used by someone who did not know about the allow_blank: true option. but this is a case for when you should use a comment on your code...

How do that the method of validation uniqueness not accept case sensitive

I have the following problem. I have the model with the following validation
validates :company, presence:true,
uniqueness: true,
format: /^([a-zA-z]+\s?){1,}$
if in the database there is stored a company with the value "Nevada" and after I add a new company with the value "nevada" that validation passes because "Nevada" is not than same that "nevada". How can I do for that the validation not accept case sensitive
You can dfine if it checks for case sensitive
validates :company, presence:true,
uniqueness: {case_sensitive: false},
format: /^([a-zA-z]+\s?){1,}$
Check also rails unique validation

Resources