Forum: Ruby on Rails Creating a new virtual field

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Marius Hächler (Guest)
on 2006-05-29 12:18

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

def self.to_new_object_name
  self.new_object_name = self.object_nr + " - " + self.object_name

I don't know what to do know, can somebody help me?
Pete Y. (Guest)
on 2006-05-30 03:35
(Received via mailing list)

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}"

Then in your controller:

   @objects = Object.find(:all)

And in your view:

   options_from_collection_for(@objects, 'id', 'new_object_name')


Pete Y.
Marius Hächler (Guest)
on 2006-05-30 10:44
Hello Pete,

Thanks to you, this works great!
This topic is locked and can not be replied to.