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 }
Related
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.
I have a post model and a keyword model (with a name attribute), and a post has_many keywords.
I want to validate the uniqueness of keywords, but relatively to its post, not to all keywords.
What I mean is: first_post and second_post can both have the keyword apple, but they can't have it twice. I'd like to avoid the duplication.
If I just add in the keyword model:
validates :name, uniqueness: true
It will check uniqueness of name among all the keywords.
How can I precise that it should only be for its post ?
EDIT:
I did add:
validates :name, uniqueness: { scope: post_id }
to the keyword.rb file.
I now get an error:
undefined local variable or method `post_id' for #<Class:0x007f8fa46b7890>
But my keyword model has a post_id attribute. Any idea on what could be causing this ?
You can use scope.
:scope - One or more columns by which to limit the scope of the
uniqueness constraint.
validates :name, uniqueness: { scope: :post_id }
I'm trying to implement a validation for a polymorphic association, where I only want it to trigger on a certain type. Which is user.
I'd want something like this:
validates :room_id, uniqueness: { scope: tokenable_id if tokenable type is User }
how do I go about doing this. The other type is Customer. Which I want to allow the opportunity to be several.
I think you can use Conditional Validation
Sometimes it will make sense to validate an object only when a given
predicate is satisfied. You can do that by using the :if and :unless
options, which can take a symbol, a string, a Proc or an Array. You
may use the :if option when you want to specify when the validation
should happen. If you want to specify when the validation should not
happen, then you may use the :unless option.
5.1 Using a Symbol with :if and :unless
You can associate the :if and :unless options with a symbol
corresponding to the name of a method that will get called right
before validation happens. This is the most commonly used option.
class Order < ActiveRecord::Base
validates :room_id, uniqueness: { scope: :tokenable_id }, if: :is_right_type?
# or maybe this will work
validates :room_id, uniqueness: { scope: :tokenable_id }, if: :user?
def is_right_type?
type == "user"
end
end
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
I have two params :work and :grade. In the model, before saving I want to use validates_uniqueness_of to check given a unique work, there is only one grade. Grade can be the same for other work. How would I write this?
Edit:
validates_uniqueness_of :work, :scope => :grade
If you have a deprecated syntax warning, you can write it so:
validates :work, uniqueness: {scope: :grade}, presence: true
Edit:
It seems you need a two way checking, so perhaps adding this will work:
validates :grade, uniqueness: {scope: :work}, presence: true
Although under high load I've seen this fail, so best is to create a database constraint.