How can one Validate presence of only one? - ruby-on-rails

I have a user model that has_one signup.
I'm having a recurring bug where users will click multiple times and create many unnecessary signups. How can I validate the user only gets one? Is there a way to do that from the model?

Not sure about your model, but validates_uniqueness_of might be what you are looking for.

In rails 3 you could do like this.
validates :field, :uniqueness => true
If you want your custom message then
validates :field, :uniqueness => {:message => 'your message'}

Related

Ruby on Rails: Getting validates_uniqueness_of to work

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.

how to do validation for extra field added in user model for devise

I have searched for this question but not found an answer.
I am using devise for creating new user.
When i am creating a new user i have some fields which are required.
I have made entry of these fields also in user model like -
validates_presence_of :first_name, :last_name, :company
I have write in user model for validate presence of these fields, but devise is only considering validation for email field and not for other fields when i,m trying to create a new user.
I have also tried removing :validatable from user model and writing customized error validation but this also not working.
However i want to validate all require fields when i am creating a new user.
How to do this.
Thanks in advance??
In your model add
validates :first_name, presence: true
validates :last_name, presence: true
something give like this..
this is rails 3.2 validation, let me know what is your rails version
validates :first_name,:last_name, :presence=> { :message => "Field is required" }, :on => :create

Error: "Unknown validator: 'PresencesValidator'" when trying to validate a form

I'm working on my first rails project and i've built a small site using rails and now i'm trying to follow the getting started with rails tutorial to build a blog.
I'm stuck on 6.5 of http://guides.rubyonrails.org/getting_started.html when trying to validate my blog post and this is the error that I get:Unknown validator: 'PresencesValidator'.
Here's my post model
class Post < ActiveRecord::Base
validates :name, :presence => { :message => "Name cannot be blank" }
validates :title, :presence => { :message => "Title cannot be blank" }
validates :content, :presences => { :message => "Content cannot be blank" }
end
I've also tried just setting :presence => true, but same error.
Why am i getting this error and how do i fix it?
Because this is a typo:
validates :content, :presences => { :message => "Content cannot be blank" }
You wrote presences when you meant to write presence.
Also, the message you've given on these is the default, so you actually don't need to provide it.
One last thing, if you want to validate the presence of three things you could do it in one line via:
validates_presence_of :name, :title, :content
That would accomplish the same exact behavior as the code you were trying to write. There are two validation syntaxes, the one you've been using is better for when you have a lot of validations on each attribute, I personally think the other kind is better when you have many different attributes you want to run the same validation on. Basically whichever requires less typing.
Take a look at the Validations and Callbacks guide as a reference. Cheers.

Rails 3 Validation on a group of fields

I have a model with a whole bunch of fields. Not all fields are used based on the user selecting a certain type of form. I have around 6 different types of forms so a field may be used on 4 of them.
Is there a way to group validation based on a element ie?
case xxx
when "form1"
validates :field1, :presence => true
when "form2"
validates :field1, :presence => true
when "form3"
validates :fiel2, :presence => true
end
I will be doing client side validation but I obviously will need server side as well to make sure they have submitted good data.
Any suggestions how this can be done ?
I'm using Rails3 with Mongoid 2.0
Thanks in advance!
Something like this?
validates :field1, :presence => true, :if => Proc.new { |foo| %w{form1 form2}.include?(foo.xxx) }
validates :field2, :presence => true, :if => Proc.new { |foo| %w{form1 form3}.include?(foo.xxx) }
validates :field3, :presence => true, :if => Proc.new { |foo| %w{form2 form3}.include?(foo.xxx) }
I see a problem with the model class having to have intimate knowledge of the views involved. If the forms in the views were named differently, the solution won't work. You will want to use "validation groups" like that used in ASP.NET. You could do some search on that and either find a similar solution for Rails or roll your own. Maybe this one will help: https://github.com/akira/validationgroup

Best way to check if a username exists in a model

I'm quite new to ruby/rails. I was wondering what is the best way to ensure that two people don't choose the same username. Here is my model at the moment:
class User < ActiveRecord::Base
validates :username, :presence => true
validates :password, :presence => true, :length => { :minimum => 7}
end
Note: I'm assuming it is best to place this type of code in the model. Correct me if I'm wrong.
There's a validation to make sure a field is unique. Just change your username validation to:
validates :username, :presence => true, :uniqueness => true
You should also add an index to your usertable, with uniqueness. This way, if people quickly press the username register button twice, you will also be protected at the database level
add_index :users, :username, :unique => true
This question has already been correctly answered but for future reference, APIDock has excellent Rails documentation here: http://apidock.com/rails. The search's autocomplete is fantastic.
The documentation for the validates method is here: http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates where you can find the :uniqueness => true option.

Resources