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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:04:36+00:00 2026-05-21T02:04:36+00:00

In the app I am building I have a need to combine multiple text

  • 0

In the app I am building I have a need to combine multiple text fields into a single database column.

For example my “Business” entry has a column “Discount”

The text field I want to read something like this:

<%= f.text_field :discount %> % Off <%= f.text_field :discount %>.  

I want both of these to be entered into the database as a string: “10% Off Shoes” (or whatever).

Is there a way to do this in Rails 3?

Thanks!

**Edit!

I tried Pan Thomakos’s solution (using virtual attributes) and now I am getting the following error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.split
Extracted source (around line #3):

1: 
2: <%= f.label :cost %><br />
3: <%= f.text_field :percentage %> % Off <%= f.text_field :product %>.


app/models/business.rb:11:in `percentage'

I’m not really sure how to handle this! Admittedly I am weak when it comes to working within the model, I probably would have handled this in the controller.

Thanks!

  • 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-21T02:04:37+00:00Added an answer on May 21, 2026 at 2:04 am

    Yes, the best way to do it is to use virtual attributes. Each virtual attribute will keep track of the different parts of the discount and the discount will be the combined field. Here is how I would implement it:

    class Business
      attr_writer :percentage, :product
    
      before_save :create_discount
    
      def percentage
        @percentage.nil? ? discount.to_s.split('% Off ').first : @percentage
      end
    
      def product
        @product.nil? ? discount.to_s.split('% Off ').last : @product
      end
    
      protected
    
      def create_discount
        discount = "#{@percentage}% Off #{@product}" unless @product.nil? || @percentage.nil?
      end
    end
    

    You can then modify your view to:

    <%= f.text_field :percentage %> % Off <%= f.text_field :product %>.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an Android app and I need to have two different themes for
I am building a Backbone app and I need to have automated tests. I
I have am building a win forms app which will have a text box
In a rails 2 app I'm building, I have a need to update a
I have an app i building which is a simple naviagtion app. I do
I am building an app and want to have a lot of the site
I am building an app to learn Django and have started with a Contact
I have a relatively small app that Im building using vb.net 2.0, and nant.
I am building an app using storyboard. I have added a table view to
I'm building an app around Vici Coolstorage (asp.net version). I have my classes created

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.