Checking rails 3 in examples of model methods

I want to call the verification method of the active record in my usual verification method, for example

class Asset < ActiveRecord::Base validate :ensure_unique_identification_code validates :name,:uniqueness=>true def ensure_unique_identification_code self.identifier="identifier code" #code is generated using some helper method of Asset model validates :identifier ,:uniqueness=>true end end 

give an error

 undefined method `validates' for #<Asset:0xb6692dbc> 

how can we call validation methods in model instance methods

+4
source share
3 answers
+2
source

You may be using the new version of Rails (3.xx) (rails -v), but you still have the old Rails application ... You should generate the Rails application in a different folder and then move the .rb files, views, etc. d. in your new application / folder ... I confirm that "validates are unique to 3, as a rule, when this error occurs, people are still 2.3", as a3uge said.

0
source

You can directly configure a specific validation method and directly call the validate () method:

 def age_validation ActiveModel::Validations::NumericalityValidator.new( :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100, :attributes => :age ).validate(self) end 
0
source

Source: https://habr.com/ru/post/1348104/


All Articles