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

  • Home
  • SEARCH
  • 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 7087009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:37:15+00:00 2026-05-28T07:37:15+00:00

To set validation inside a Model in Rails, I should write something like: class

  • 0

To set validation inside a Model in Rails, I should write something like:

class Post < ActiveRecord::Base
  validates :name,  :presence => true
  validates :title, :presence => true,
                    :length => { :minimum => 5 }
end

I can’t figure out how this works. It seems like it is calling a method named validates and is passing parameters, but this can’t be because, I believe I cannot call a method directly in a class body.

So what is actually happening in here?

update

From the answers it seems like this is calling a method from the inhereted Base class but then why does this not work?:

class Parent
  def foo
    puts "called foo"
  end
end

class Child < Parent
 foo
 foo
end
  • 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-28T07:37:16+00:00Added an answer on May 28, 2026 at 7:37 am

    You wrote “but this can’t be because (I believe) you cannot call a method directly in a class body.”

    But that’s not true — code is being executed as the class is loaded

    Consider this:

    class Hi
      puts "yo"
    end
    

    You’ll get:

    yo
     => nil
    

    the puts is executed when it’s loaded. So you can use this to create other methods or do whatever you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My team is building ViewModels with model validation inside the MetadataType. My question is
I´m having problem using validation on DropDownListFor ... My Model : public class User
My Model: public class Country { public int CountryId { get; set; } public
I have a range named STATE. I want to set data validation in range
I traditionally deploy a set of web pages which allow for manual validation of
I need to set the ErrorMessage property of the DataAnnotation's validation attribute in MVC
there are many posts that deal with validation controls inside update panel and partial
I am trying to write a simple input field validation plugin at the moment
Currently, I create an object and use its setters to set the get/post data
My User (Devise) model also has name, city, nation, phone members. In the create

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.