Skipping Couple of Validate Presence in model

23 views
Skip to first unread message

honey ruby

unread,
Oct 14, 2015, 3:56:01 AM10/14/15
to Ruby on Rails: Talk
Hi everyone,

In a User model has 4 columns mandatory I have used validate presence true first_name, last_name, username, country.
Now if the user is admin and when I want to update his details I don't want to check country I want validate presence part from country. In this case how can I do it. I want to update admin who is also user in User model but I want to skip couple of column and need to check rest of the columns to validate presence.

Kindly let me know how it can be done.

Cheers!!!

honey ruby

unread,
Oct 14, 2015, 4:08:40 AM10/14/15
to Ruby on Rails: Talk
am using Rails 3.2.21 I forgot to mention that.

Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/407d73af-950a-4b36-a2a9-6518fb9e7aa2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Oct 14, 2015, 4:30:58 AM10/14/15
to Ruby on Rails: Talk
On 14 October 2015 at 08:56, honey ruby <emailtoh...@gmail.com> wrote:
> Hi everyone,
>
> In a User model has 4 columns mandatory I have used validate presence true
> first_name, last_name, username, country.
> Now if the user is admin and when I want to update his details I don't want
> to check country I want validate presence part from country. In this case
> how can I do it. I want to update admin who is also user in User model but I
> want to skip couple of column and need to check rest of the columns to
> validate presence.

Have a look at
http://guides.rubyonrails.org/v3.2.21/active_record_validations_callbacks.html#conditional-validation
which shows you how to use conditional validations.

Colin

>
> Kindly let me know how it can be done.
>
> Cheers!!!
>

honey ruby

unread,
Oct 14, 2015, 7:42:08 AM10/14/15
to Ruby on Rails: Talk
Thanks colin for your reply.

I have used this way in my model.

  validates :first_name, :last_name, :username, :next_of_name, :phone, :email presence: true
  validates :first_name, :last_name, :username,:password, :password_confirmation, :presence => true, if: :only_for_admin

  def only_for_admin
    self.admin
  end

when I save things for admin... phone and next_of_name those fields are not allowing to save.  Correct me if is not correct process.
 

Colin Law

unread,
Oct 14, 2015, 8:31:56 AM10/14/15
to Ruby on Rails: Talk
On 14 October 2015 at 12:41, honey ruby <emailtoh...@gmail.com> wrote:
> Thanks colin for your reply.
>
> I have used this way in my model.
>
> validates :first_name, :last_name, :username, :next_of_name, :phone,
> :email presence: true
> validates :first_name, :last_name, :username,:password,
> :password_confirmation, :presence => true, if: :only_for_admin
>
> def only_for_admin
> self.admin
> end
>
> when I save things for admin... phone and next_of_name those fields are not
> allowing to save. Correct me if is not correct process.

The first line will be executed first, it says validate presence of
everything whether admin or not, so the save fails. That line should
only specify the fields that are always required.
The second line should then say validate the conditional fields, but
they only *not* admin, or unless admin.

By the way, I would prefer it if you could insert your replies into
the previous message rather than top posting. It makes it easier to
follow the thread. Thanks.

Colin
> https://groups.google.com/d/msgid/rubyonrails-talk/CAOKUzCnoURubAf%3DPxmJ1kPP4v2Ogp5t0%3Dos0WrO2c3Tu7HNojA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages