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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:25:38+00:00 2026-05-16T00:25:38+00:00

I have attribute name of model Person. I want to use html-form with fields:

  • 0

I have attribute name of model Person.

I want to use html-form with fields: first_name and surname:

<%= f.text_field first_name%>
<%= f.text_field surname%>

And I want to compose these virtual attributes to model attribute name.

What’s the best way to do it?

I tried to use composed_of, but failed…

class Person < ActiveRecord::Base
  composed_of :name, :class_name => 'Name', :mapping => %w(name output_name)
end

class Name
  attr_reader :output_name
  def initialize(first_name, surname)
    @output_name = first_name + surname
  end
end

@person.attributes= {"name(1s)" => 'Alex', "name(2s)" => 'Bolduin' }
@person.name.should == 'Alex Bolduin'

expected: "Alex Bolduin",
got: #<Name:0x000000049d4c08 @output_name="Alex Bolduin"> (using ==)

I use Ruby on Rails 3, but I think solution is similar for Rails 3 and Rails 2.3

  • 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-16T00:25:38+00:00Added an answer on May 16, 2026 at 12:25 am

    You should be able to make a pretend column doing something like this in the model:

    class Person < ActiveRecord::Base
        def name
          "#{self.first_name} #{self.surname}"
        end
    
        def name=(fullname)
          first,last = fullname.split(' ')  # or some smarter way to split
          self.first_name = first
          self.surname = last
        end
    end
    

    It should be accessible the same way as any other column, and when you try to set name it will just call the setter methods for first_name and surname.


    If you want to do the opposite, you can still use the same trick:

    class Person < ActiveRecord::Base
        def first_name
          name.split(' ')[0]
        end
    
        def surname
          name.split(' ')[1]
        end
    
        def first_name=(first)
          name = [first, name.split(' ')[1]].join(' ')
        end
    
        def surname=(sname)
          name = [name.split(' ')[0], sname].join(' ')
        end
    
    end
    

    The only thing potentially bad about this is that string manipulation occurs once for each read and twice for each write. You could alternatively catch things in the controller by adding something like this to create/update before @person.save:

    @person.name = [first_name, surname].join(' ')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following model: Person <AR def name [self.first_name,self.middle_name,self.last_name].select{|n| n.present?}.join(' ')
I have this attribute in my view model: [CustomRequired, EmailRegex] [Display(Name = KeepInformedPersonMail, ResourceType
So I have a city model. Each city has a Name attribute as well
I have a basic model: class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) state
I have two model classes, Person and Relationship as follows with fields omitted for
I have created an object: var Person = Backbone.Model.extend({ defaults: { 'name': '', 'age':
Is it possiible to change attribute name on db.Model kind? i have some field
I have a PaymentDetail model with attribute 'home_address_country', so i can use @payment_detail.home_address_country //where
I have a input field with a name attribute like field[subkey][subkey2] How can I
I have an attribute declared on property. How can I get the property name

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.