# Creating a new virtual field

**URL:** <https://www.ruby-forum.com/t/creating-a-new-virtual-field/61883>\
**Category:** Rails\
**Created:** [May 29, 2006, 10:18am UTC](https://www.ruby-forum.com/t/creating-a-new-virtual-field/61883 "2006-05-29T10:18:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marius\_HSSS\_chler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@Marius\_HSSS\_chler](https://www.ruby-forum.com/u/Marius_HSSS_chler)\
**Post date:** [May 29, 2006, 10:18am UTC](https://www.ruby-forum.com/t/creating-a-new-virtual-field/61883/1 "2006-05-29T10:18:13Z")

</div>

Hello,

I have a problem with one of my Rails-Application, I have a table and on  
this there is object\_name and a object\_nr, so i want to create a  
combination of both in the model for a collection box.

I’ve tried something like this:

def set\_new\_object\_name  
objects = find(:all).to\_new\_object\_name  
end

def self.to\_new\_object\_name  
self.new\_object\_name = self.object\_nr + " - " + self.object\_name  
end

I don’t know what to do know, can somebody help me?

---

<div class="post-metadata">

**Author:** ![Marius\_HSSS\_chler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@Marius\_HSSS\_chler](https://www.ruby-forum.com/u/Marius_HSSS_chler)\
**Post date:** [May 30, 2006, 1:35am UTC](https://www.ruby-forum.com/t/creating-a-new-virtual-field/61883/2 "2006-05-30T01:35:59Z")

</div>

Marius,

If I understand correctly what you’re trying to do, you want  
something like this in your model:

def new\_object\_name  
“#{object\_nr} - #{object\_name}”  
end

Then in your controller:

@objects = Object.find(:all)

And in your view:

options\_from\_collection\_for(@objects, ‘id’, ‘new\_object\_name’)

Cheers,

Pete Y.  
[http://9cays.com/](http://9cays.com/)

---

<div class="post-metadata">

**Author:** ![Marius\_HSSS\_chler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@Marius\_HSSS\_chler](https://www.ruby-forum.com/u/Marius_HSSS_chler)\
**Post date:** [May 30, 2006, 8:44am UTC](https://www.ruby-forum.com/t/creating-a-new-virtual-field/61883/3 "2006-05-30T08:44:26Z")

</div>

Hello Pete,

Thanks to you, this works great!
