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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:07:51+00:00 2026-05-13T23:07:51+00:00

I have the following validation in a model: validates_length_of :description, :minimum => 2, :on

  • 0

I have the following validation in a model:

validates_length_of :description,
  :minimum => 2, :on => :save,
  :message => "must be at least 2 words",
  :tokenizer => lambda {|text| text.scan(/\w+/)}

And this works fine. When I add a second field to the model that needs to be validated by number of words, I declare

tokenize_by_words = lambda {|text| text.scan(/\w+/)}

at the top of the model, and use

:tokenizer => tokenize_by_words

This also works fine, and keeps everything DRY. However, it all falls apart when I need to use the same tokenizer across multiple models. If I create config/initializers/tokenizers.rb thus:

class ActiveRecord::Base
  tokenize_by_words = lambda {|text| text.scan(/\w+/)}
end

and remove the definitions in the models, I get /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1959:in 'method_missing': undefined local variable or method 'tokenize_by_words' for #<Class:0x10357e988> (NameError)

Using an instance variable or replacing the whole thing with a method doesn’t work either.

I’m sure there’s something blindingly obvious I’m missing, but the only documentation I can find on :tokenizer doesn’t really consider DRY-ness 🙁

  • 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-13T23:07:51+00:00Added an answer on May 13, 2026 at 11:07 pm

    You should define the tokenizer as a method, i.e.

    class ActiveRecord::Base
      def foo(text)
        text.scan(/\w+/)
      end
    end
    

    Now use this method symbol as the value for :tokenizer attribute.

    validates_length_of :description,
      :minimum => 2, :on => :save,
      :message => "must be at least 2 words",
      :tokenizer => :foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a model say Task , I have following validation validates_presence_of :subject, :project, :user,
I have a url field named link in my model with the following validation
I have the following validation in my model ( User ): validates :first_name,:length =>
I have the following model field validation: validates_uniqueness_of :acronym, :scope => [:group_id], :case_sensitive =>
I have a model class that uses a custom validator as shown in following
I have the following validation summary control in the markup: <asp:ValidationSummary ID=vsValErrs CssClass=errors HeaderText=<div><%#
I have the following script validation.php <?php if(!isset($_SESSION['userId'])){ session_destroy(); header('Location: home.php'); exit; } ?>
I have the following code validation php script: if(empty($_POST['captcha_code'])) { $error = 1; $code[3]
I have been following the validation for Entry boxes from here . The code
I have following nested objects. I am using @Valid for validation in my controller.

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.