Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:50:23+00:00 2026-05-29T09:50:23+00:00

class Person < ActiveRecord::Base validates :terms_of_service, :acceptance => true end In the above, what

  • 0
class Person < ActiveRecord::Base
  validates :terms_of_service, :acceptance => true
end

In the above, what is validates from a Ruby standpoint? It’s not a method definition, it’s not a data definition or declaration. So, evidently it’s a method invocation, right there in the body of the class. I have never seen a method invoked like that directly in a class (i.e. outside of a method definition), not even in the Ruby programming tutorial I’m going through now: http://ruby-doc.org/docs/ProgrammingRuby/.

So, if it’s a method invocation, at what point is it being invoked? I tried the following as a test:

class Person   

  print "cat"
    
end

#p = Person.new
#q = Person.new

print "cat" is being executed exactly once, regardless if any actual Person objects are being declared or not, so evidently just when parsing the class definition, Ruby sees the method print and says, "OK I will just go ahead and execute this now" but never does so again.

So, where is the Ruby documentation that will help me understand what is going on with validates above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:50:24+00:00Added an answer on May 29, 2026 at 9:50 am

    In Ruby, class declarations are just chunks of code, executed in order.

    It’s important to remember that inside a class definition, self points to the class itself. validates is a class method of ActiveRecord. As the class is being defined, code in the definition is executed. The validates method resolves to a class method of ActiveRecord, so is called during class definition.

    In your Person example, it will only print once, because you only define the class once.

    Consider the following:

    class Foo
      def self.validates_nothing(sym)
        (@@syms ||= []) << sym
        puts "!!! Here there be logic"
      end
    
      def validate
        @@syms.each { |s| puts s }
      end
    end
    

    This defines a class with a class method validates_nothing, and an instance method, validate. validates_nothing just gathers whatever arguments are given it, validate just dumps them out.

    class Bar < Foo
      validates_nothing :anything
      validates_nothing :at_all
    end
    

    This defines a subclass. Note that when the class method validates_nothing is called, it prints:

    Here there be logic
    Here there be logic
    

    If we create a new bar and call validate, we get the expected output:

    > Bar.new.validate
    !!!anything
    !!!at_all
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my data models class Person < ActiveRecord::Base end class Landlord < Person end
class Party < ActiveRecord::Base belongs_to :hostess, class_name: 'Person', foreign_key: 'hostess_id' validates_presence_of :hostess end class
I have: class Person < ActiveRecord::Base has_many :people_phones has_many :phones, :through => :people_phones end
I have two models: Company and Person class Person < ActiveRecord::Base belongs_to :company end
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
What does ::Base part mean in Person < ActiveRecord::Base class declaration? I'm new to
I have the following setup: class Publication < ActiveRecord::Base has_and_belongs_to_many :authors, :class_name=>'Person', :join_table =>
Is it true that HABTM relationships do not support the :dependent option? class Person
If I have a Model like the following example class Person < ActiveRecord::Base has_many
I have the following two models class ContactField < ActiveRecord::Base end class Address <

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.