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 9122721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:10:10+00:00 2026-06-17T06:10:10+00:00

In custom validation methods, why are the attributes passed as local variables instead of

  • 0

In custom validation methods, why are the attributes passed as local variables instead of being accessible as instance variables?

I was expecting to use @title instead of title in the custom validation below, but @title is nil in the code below. title contains the actual data.

    attr_accessible :title
    validate :do_check_title

    def do_check_title
      title =~ /^Alice in/ || errors.add(:title, "Not Alice in Wonderland")
    end

Looking through the active_record/core.rb

   def initialize(attributes = nil)
     ...
     assign_attributes(attributes) if attributes
     ...
   end

And then in active_record/attribute_assignment.rb

   def _assign_attribute(k, v)
     public_send("#{k}=", v)

So I guess, the attributes should be available as instance variables in the validation function.

Why are they nil?

  • 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-06-17T06:10:12+00:00Added an answer on June 17, 2026 at 6:10 am

    It doesn’t really matter if you access these from a validation or other instance methods. You can see what’s happening from the console (pry):

    u = User.first
    show-method u.first_name=
    

    This gives you something like this:

    generated_attribute_methods.module_eval("def #{attr_name}=(new_value); write_attribute('#{attr_name}', new_value); end", __FILE__, __LINE__)
    

    Now if you take a look at the write_attribute method, you can see that it deletes attribute cache etc. and then assign to attributes and it’s just a hash.

    u.attributes
    

    So from now on, instead of the boring u.first_name = "foo", you can use this:

    u.send :write_attribute, "first_name", "foo"
    

    and it will do the same thing (3.2.10).

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

Sidebar

Related Questions

I'm really really new to rails. I haven't really written any custom validation methods
HTML5 allows the use of custom attributes prefixed with the phrase data- which pass
I have setup a custom provider to allow setting validation attributes from a data
I understood how to create custom validation attributes. In fact, the Validate method is
Is it okay to put some custom validation methods in AppModel and call those
I added a custom validation method to validate a password. However, it does not
I have a custom validation in the enterprise validation block. The DoValidate method is
I have this custom validation attribute for validating a collection. I need to adapt
Possible Duplicate: MVC3 custom validation: compare two dates I am trying to make a
I have custom validation rule: public function customRule($check) { } Inside this rule I

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.