How do that the method of validation uniqueness not accept case sensitive - ruby-on-rails

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

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.

How to validate bank details like account_number, ifsc_code, bank_name and branch_code etc... in Rails

In my rails application, I have bank_details table with columns - account_number, ifsc_code, bank_name and branch_code(Only for India). But I don't know how to validates this columns before save it to database.Plese help me in this.
I will appreciate your help.
Thanks in advance.
In your model, you need to add a validates: your_column.
For example if you want to make sure that account number is present before you save to the database, you need to add validates: account_number, presence: true
There are both pre-built helpers, so you can validate by presence, name, etc, but you can also use a custom method to validate those.
Check out RailsGuides for all the details
There are so many validation that you can apply on your columns, here is a list of possible validation. you can pick as your requirement.
Validation for data should be present
validates :account_number, :ifsc_code, :branch_code, :bank_name, presence: true
data should be positive numbers only
validates :account_number, :branch_code, :numericality => { :greater_than_or_equal_to => 0 }
set length of record
validates :ifsc_code, length: { is: 11 }
validates that the attribute's value is unique right before the object gets saved
validates :account_number, uniqueness: true
In your model you can have something like this:
validates: branch_code
which is a standard validation, making sure its present
Or you can do more validation using regex
validates :bank_name, format: { with: /\A[a-zA-Z]+\z/,
message: "only allows letters" }
Here are some examples of validation:
validates :bank_name, length: { minimum: 2 }
validates :bank_name, length: { maximum: 500 }
validates :bank_name, length: { in: 6..20 }
validates :bank_name, length: { is: 6 }
Or use some advance techniques like so:
validates :bank_name, presence: true, if: "branch_code.nil?"
TransferWise provides an API you can use to validate IFSC codes and Indian account numbers:
IFSC code
https://api.transferwise.com/v1/validators/ifsc-code?ifscCode=YESB0236041
Account number
https://api.transferwise.com/v1/validators/indian-account-number?accountNumber=678911234567891
Documentation here.

How to create custom unique validation

I want to create custom unique validation.
There is three elements of type, ['top', 'middle',bottom'].
Name have just three of them.
How should I create validation for this?
validates :name, uniqueness: {scope: [:type]}
It looks good to me. Just, you don't need an array because you scope for just one value.
validates :name, uniqueness: { scope: :type }

Datatype model validation

In Ruby on Rails, I can't seem to find a validation model method to check for data-type.
I was hoping for something like
validates :name, datatype: :integer
but there is nothing on http://guides.rubyonrails.org/testing.html
If such a test not required? Is it because the database engine (mysql or whatever) itself would reject information with a wrong data-type?
It seems you are new to Rails there are already predefined validations in rails Ref Active Record Validations in Rails
validates :name, numericality: true
or
validates :name, numericality: { only_integer: true }
You can do something like
validates :your_field, :numericality => { :greater_than_or_equal_to => 0 }
please check the link
Validation for non-negative integers and decimal values
There is a validation for this in Rails
validates_numericality_of :value, :only_integer: true

Rails 3: how to generate custom error message from failed validation

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"}

Resources