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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:20:24+00:00 2026-05-23T14:20:24+00:00

I’ve created a custom validator in Rails 3.0 which validates whether a combination of

  • 0

I’ve created a custom validator in Rails 3.0 which validates whether a combination of columns is unique within a table. The entire code of the validation is:

class UniqueInProjectValidator < ActiveModel::EachValidator

  def validate_each(object, attribute, value)
    unless object.class.where("project_id = ? AND #{attribute} = ?", object.project_id, value).empty?
      if object.new_record?
        object.errors[attribute] << (options[:message] || "must be unique in each project")
      else
        orig_rec = object.class.find(object.id)
        if value != orig_rec.method(attribute).call || object.project_id != orig_rec.project_id
          object.errors[attribute] << (options[:message] || "must be unique in each project")
        end
      end
    end

end

Note that it is not easy to recognize what the if statements do, so I was hoping to be able to replace the unless conditional with a def attribute_and_project_exist? method and the second if statement with a def attribute_or_project_changed? method. However when creating those methods, the arguments from validates_each do not pass because of encapsulation.

Now the question: Is there a way to somehow cleanly allow those variables to be accessed by my two newly created methods as one can do with column names in a model, or am I stuck with the options of either passing each argument again or leaving the hard to read conditional statements?

Thanks in advance!

  • 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-23T14:20:24+00:00Added an answer on May 23, 2026 at 2:20 pm

    I suppose you could clean it up a bit with one variable, one lambda, and one “return as soon as possible”:

    def validate_each(object, attribute, value)
      # If there is no duplication then bail out right away as
      # there is nothing to check. This reduces your nesting by
      # one level. Using a variable here helps to make your
      # intention clear.
      attribute_and_project_exists = object.class.where("project_id = ? AND #{attribute} = ?", object.project_id, value).empty?
      return unless attribute_and_project_exists
    
      # This lambda wraps up your second chunk of ugly if-ness and saves
      # you from computing the result unless you have to.
      attribute_or_project_changed = lambda do
        orig_rec = object.class.find(object.id)
        value != orig_rec.method(attribute).call || object.project_id != orig_rec.project_id
      end
    
      # Note that || short-circuits so the lambda will only be 
      # called if you have an existing record.
      if object.new_record? || attribute_or_project_changed.call
        object.errors[attribute] << (options[:message] || "must be unique in each project")
      end
    end
    

    I don’t know how much better that is than your original but the logic and control flow is a lot clearer to me due to the nicer chunking.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has

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.