On Jun 22, 2005, at 7:22 PM, Corey Lawson wrote:> I have this structure:
>
> create table party (
> id int autonum not null primary key,
> type varchar(50),
> name varchar(50),
> firstname varchar(50),
> lastname varchar(50)
> )
>
> app\models\party.rb:
> class Party << ActiveRecord::Base
> ...
> end
>
I don''t see it explicitly (perhaps in the <snipped> part of your
class?) but the table name is usually pluralized (create table
parties) so that the model can autodetect the name (Part).
> app\models\person.rb
> class Person < Party ''type = Person
> def name ''fill in the empty "name" field
> read_attribute("firstname") + " " +
read_attribute("lastname")
> end
> end
>
You can do this inside your model:
def name ''fill in the empty "name" field
read_attribute("firstname") + " " +
read_attribute("lastname")
rescue nil
end
> 3: <!--[form:person]-->
> 4: <p><label for="person_name">Name</label>
> 5: <%= content_tag "br", @person.name %>
> 6:
> 7: <p><label
for="person_firstname">Firstname</label><br/>
> 8: <%= text_field ''person'',
''firstname'' %></p>
>
> I imagine I have to do some kungfu (i.e., something like
> <%= content_tag "br", @person.exists? @person.name :
"") %>
> What is really the correct syntax?
If you add the "rescue nil" to your model, then in your views you can
do this:
<%= content_tag "br", (@person.name || "[none]") %>
Alternatively, if you want to handle the exception in your view, (in
this case not placing the rescue in the model), you could do this:
<%= content_tag "br", (@person.name rescue "[none]")
%>
> Also, is there any way to force a given column (i.e., named
''type'') to
> not be classified as a "system'' column when using
> Object.content_columns? I tried overriding the method in the model,
> but that didn''t seem to really help.
You can either override the content_columns method in
ActiveRecord::Base (not recommended) or you can do this in your model:
class Person < Party
set_inheritance_column "field"
end
Where "field" is something other than "type" (I
don''t think it has to
actually be a valid field name, and you might possibly be able to set
it to "" or nil).
Duane Johnson
(canadaduane)
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails