On Tue, 8 Mar 2005 16:56:19 -0600, Joshua Wehner
<joshua-AnsgPkSrJBbs2Lz0fTdYFQ@public.gmane.org>
wrote:> Sorry if this is a really lame question, I''m sure it''s
something fairly
> obvious, but I just can''t see it at the moment...
>
> I''m trying to add something simple to the model for a project
> management/todo list/thing. The DB has ''first_name'' and
''last_name'',
> and I''d like to have "full_name" available.
>
> So - models/owner.rb:
>
> def self.full_name
> first_name + '' '' + last_name
> end
>
> Then, in projects/edit.rhtml:
>
> <p><label
for="project_owner_id">Owner</label><br /><select
> id="project_owner_id"
name="project[owner_id]"><%>
options_from_collection_for_select @owners, "id",
"full_name",
> @project.owner_id %></select>
>
> When I try to access this, I get "NoMethodError" for full_name.
Change it to def full_name... etc.
By adding the self. in front of full_name, you''re specifying it as a
Class Method (ie: one that you would access like Owner.full_name as
opposed to @owner.full_name).
Pragmatic Ruby has a great section on Ruby classes and objects:
http://www.rubycentral.com/book/tut_classes.html
--
rick
http://techno-weenie.net