0

I have the following in my models/user.rb:

validates :company, presence: true
validates :title, presence: true

I have a secondary view where I want to create a user but not require this user to enter a company and a title. How would I do that without modifying the main user.rb?

This is for Rails 3.2

Kostas Rousis
  • 5,918
  • 1
  • 33
  • 38
EastsideDev
  • 6,257
  • 9
  • 59
  • 116
  • In that case you can use the client side validations like jQuery validation plugin on just one view rather than having it declared in model itself. – uday May 20 '14 at 19:25

2 Answers2

1

You can do by declaring custom validations the way @BroiSatse has answered or when saving the user you can pass validate: false as argument, do this way

@user.save(:validate => false)
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
0

I usually do sth like:

class User < AR::Base

  validates :company, :title, presence: true, if: :validate_company_and_title?

  def validate_company_and_title?
    @validate_company_and_title.nil? || @validate_company_and_title
  end

  def skip_company_and_title_validation!
    @validate_company_and_title = false  
  end
end

Then in your controller create action for given view you can do:

@user.skip_company_and_title_validation!
BroiSatse
  • 44,031
  • 8
  • 61
  • 86