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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:12:30+00:00 2026-06-15T23:12:30+00:00

In my form I have a virtual attributes that allows me to accept mixed

  • 0

In my form I have a virtual attributes that allows me to accept mixed numbers (e.g. 38 1/2) and convert them to decimals. I also have some validations (I’m not sure I’m handling this right) that throws an error if something explodes.

class Client < ActiveRecord::Base
  attr_accessible :mixed_chest

  attr_writer :mixed_chest

  before_save :save_mixed_chest

  validate :check_mixed_chest

  def mixed_chest
    @mixed_chest || chest
  end

  def save_mixed_chest
    if @mixed_chest.present?
      self.chest = mixed_to_decimal(@mixed_chest)
    else
       self.chest = ""
    end
  end

  def check_mixed_chest
    if @mixed_chest.present? && mixed_to_decimal(@mixed_chest).nil?
      errors.add :mixed_chest, "Invalid format. Try 38.5 or 38 1/2"
    end
  rescue ArgumentError
    errors.add :mixed_chest, "Invalid format. Try 38.5 or 38 1/2"
  end

  private

  def mixed_to_decimal(value)
    value.split.map{|r| Rational(r)}.inject(:+).to_d
  end

end 

However, I’d like to add another column, wingspan, which would have the virtual attribute :mixed_wingspan, but I’m not sure how to abstract this to reuse it—I will be using the same conversion/validation for several dozen inputs.

Ideally I’d like to use something like accept_mixed :chest, :wingspan ... and it would take care of the custom getters, setters, validations, etc.

EDIT:

I’m attempting to recreate the functionality with metaprogramming, but I’m struggling in a few places:

def self.mixed_number(*attributes)
  attributes.each do |attribute|
    define_method("mixed_#{attribute}") do
      "@mixed_#{attribute}" || attribute
    end
  end
end

mixed_number :chest

This sets chest to “@mixed_chest”! I’m trying to get the instance variable @mixed_chest like I have 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-06-15T23:12:32+00:00Added an answer on June 15, 2026 at 11:12 pm

    You’re going to want a custom validator

    Something like

    class MixedNumberValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        if value.present? && MixedNumber.new(value).to_d.nil?
          record.errors[attribute] << (options[:message] || "Invalid format. Try 38.5 or 38 1/2")
        end
      end
    end
    

    Then you can do

    validates :chest, mixed_number: true
    

    Note that I’d extract the mixed_to_decimal stuff into a separate class

    class MixedNumber
      def initialize(value)
        @value = value
      end
    
      def to_s
        @value
      end
    
      def to_d
        return nil if @value.blank?
        @value.split.map{|r| Rational(r)}.inject(:+).to_d
      rescue ArgumentError
        nil
      end
    end
    

    and that this definition lets you drop the if statement in the save_chest method.

    Now you just need to do some metaprogramming to get everything going, as I suggested in my answer to your other question. You’ll basically want something like

    def self.mixed_number(*attributes)
      attributes.each do |attribute|
        define_method("mixed_#{attribute}") do
          instance_variable_get("@mixed_#{attribute}") || send(attribute)
        end
    
        attr_writer "mixed_#{attribute}"
    
        define_method("save_mixed_#{attribute}") do
          # exercise for the reader ;)
        end
    
        before_save "save_#{attribute}"
        validates "mixed_#{attribute}", mixed_number: true
      end
    end
    
    mixed_number :chest, :waist, :etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model with some attributes and a virtual attribute. This virtual attribute
I have a rather unique class that allows its child classes to declare virtual
I have added a checker to ensure that all fields in a form have
I've creating one ASP web application, in that application one form have to update
I have form where user submits field. Field can have letters, numbers, and punctuation.
My problem is that iam using admin generator for model with some extra virtual
So I'm coding a registration form, and I would like to have virtual placeholders,
I have a form with fields that are not on the correspondent model. I
I have a GUI-thread for my form and another thread that computes things. The
I have two forms. On the first form I have a virtual numpad (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.