Pretty simple scenario, but spending half a day on wiki/list archives/ google/etc got me nowhere. Trying to whip together an app to help with Katrina relief efforts so your quick reply is appreciated. I have a table with ''first_name'' and ''last_name'' fields and I want to show "last, first" in a combo box in a form. I''m new to Ruby so I look up how to access variables and build strings and such and came up with adding this to the "doctor.rb" model file: def full_name "#{@last_name} ,#{@first_name}" end with this in the form of the related entity: <tr> <td><b>Doctor: </b></td> <td><select id="doctor_id" name="patientmedicine[doctor_id]"> <%= options_from_collection_for_select @doctors, "id", "full_name", @patientmedicine.doctor_id %> </select> </td> </tr> However, I just get " , " for every record in the combo box (though the correct number of records). How do I access the fields of the current object in the context of the select helper? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
@last_name is refering to the class instance member ''last_name'' (which doesn''t exist). I believe you want to use: def full_name "#{self.last_name}, #{self.first_name}" end e. On Tue, 06 Sep 2005, Colin Madere wrote:> Pretty simple scenario, but spending half a day on wiki/list archives/ > google/etc got me nowhere. Trying to whip together an app to help > with Katrina relief efforts so your quick reply is appreciated. > > I have a table with ''first_name'' and ''last_name'' fields and I want to > show "last, first" in a combo box in a form. I''m new to Ruby so I > look up how to access variables and build strings and such and came > up with adding this to the "doctor.rb" model file: > > def full_name > "#{@last_name} ,#{@first_name}" > end > > with this in the form of the related entity: > > <tr> > <td><b>Doctor: </b></td> > <td><select id="doctor_id" name="patientmedicine[doctor_id]"> > <%= options_from_collection_for_select @doctors, "id", > "full_name", @patientmedicine.doctor_id %> > </select> > </td> > </tr> > > > However, I just get " , " for every record in the combo box (though > the correct number of records). > > How do I access the fields of the current object in the context of > the select helper?
eric lindvall <eric-q/2qte4g/KTQT0dZR+AlfA@public.gmane.org> writes:> @last_name is refering to the class instance member ''last_name'' (which > doesn''t exist).Just to expand on this a bit... ActiveRecord uses the "MethodMissing" exception to resolve to the database columns. So the database fields (which you might think of as attributes) are only accessed through methods and not directly. That''s why self.last_name and self.first_name work; they are methods invoked on the object. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
On Wednesday 07 September 2005 01:40, Colin Madere wrote:> I have a table with ''first_name'' and ''last_name'' fields and I want to > show "last, first" in a combo box in a form. I''m new to Ruby so I > look up how to access variables and build strings and such and came > up with adding this to the "doctor.rb" model file: > > def full_name > "#{@last_name} ,#{@first_name}" > endChange it to def full_name "#{last_name} ,#{first_name}" end The contents of database columns are not represented by instance variables, they''re stored in a single hash and Rails synthesizes accessor methods. Michael -- Michael Schuerig Failures to use one''s frontal lobes mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org can result in the loss of them. http://www.schuerig.de/michael/ --William H. Calvin
Eric & Michael, thanks, working now. Diving into both Ruby and Rails with no experience with either, I appreciate the quick and friendly answers. Colin On Sep 6, 2005, at 8:04 PM, Michael Schuerig wrote:> On Wednesday 07 September 2005 01:40, Colin Madere wrote: > >> I have a table with ''first_name'' and ''last_name'' fields and I want to >> show "last, first" in a combo box in a form. I''m new to Ruby so I >> look up how to access variables and build strings and such and came >> up with adding this to the "doctor.rb" model file: >> >> def full_name >> "#{@last_name} ,#{@first_name}" >> end >> > > Change it to > > def full_name > "#{last_name} ,#{first_name}" > end > > The contents of database columns are not represented by instance > variables, they''re stored in a single hash and Rails synthesizes > accessor methods. > > Michael > > -- > Michael Schuerig Failures to use one''s frontal lobes > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org can result in the loss of them. > http://www.schuerig.de/michael/ --William H. Calvin > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >