The main issue of rails

  class Person < ActiveRecord::Base
    validates_uniqueness_of :user_name, :scope => :account_id
  end

my question is about the three lines of code above. By the way, I am a C ++ programmer and a new one for rubies and rails. I am very confused by the line: validates_...what is it? variable definition? function call? or something eles. For me, this is a strange line in the definition of a class.

+3
source share
3 answers

This is a function call.

The function is defined in the module (ActiveRecord :: Validations). Modules in Ruby are objects and can have functions and variables attached to them.

You can turn on the module and get it. Here it is done in your parent class: ActiveRecord :: Base. If you look at this definition, it starts with something like:

module ActiveRecord
  class Base
    include ActiveRecord::Naming
    # ...
    include ActiveRecord::Validations
    #...
  end
end

, ++ Ruby . -, Ruby . , . . : Ruby .

-, . ++ . . Ruby , . , . , . .

Ruby Metaprogramming: .

+2

. validates_uniqueness_off(:user_name, :scope => :account_id). Ruby . Rails ActiveRecord::Validations. API rails API , , , , . , .

+2

This is a function call. You can find more examples of model validation here .

0
source

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


All Articles