I''m not too sure if this will work, I''m a bit new to this
:)... but if
you change your territory method to
def territory
sales.find(:first, :id, :order=>''date DESC'')
end
then if there is no sales present then the method should return a nil.
Then in your view you could have
<%= @salesperson.territory.order_number unless @salesperson.territory.nil?
%>
<%= "No Sales Yet" if @SalesPerson.territory.nil? %>
I''d love to know if this works.
Cheers
On 9/2/05, Steve Ross
<sross-ju+vs0qJmycyYsO2DCxJlVaTQe2KTcn/@public.gmane.org>
wrote:> I''m having a terminology/technique brain-lock. This is, I know,
easy, but
> I''m jumping through all kinds of hoops to get it done.
Here''s the issue:
>
> I have a SalesPerson model (table: sales_people).
>
> Each SalesPerson has zero or more Sales (table sales, foreign_key:
> sales_person_id). When you create a new salesperson, they normally
don''t
> have sales.
>
> So...
>
> class Admin::SalesPerson < ActiveRecord::Base
> has_many :sales_people
>
> --and--
>
> class Admin::Sale < ActiveRecord::Base
> belongs_to :sales_person
>
> Here are the places I''m just not getting:
>
> 1. Typically, when I display a SalesPerson (list method), I display their
> last sale order. This can''t be done for a new SalesPerson, (the
list at the
> end of the create process), so I''ve resorted to the model code:
>
> def territory
> sales.find(:first, :id, :order => ''date
DESC'').order_number
> rescue
> ''''
> end
>
> This tries to find the chronologically most recent and if it barfs, then it
> returns an empty string. Q: Is this the best way?
>
> 2. The logic of the entire Sales model is predicated on knowing the
> SalesPerson id, so one can go to www.myfinesalessite.com/admin/sale/1 and
> get the sales for SalesPerson 1. To make this link, when you click on the
> "show" button in the SalesPerson list, it goes to
>
> :controller => ''Sale'', :action =>
''list'', :id => @SalesPerson
>
> In the case of a new sales person, there are no sales, so the list is
> empty,
> but I want to persist this id so I can stuff it into the sales_person_id
> field in the create method. Methods like list also rely on knowing this id
> to show sales for only the correct SalesPerson. What is the
"Rails" way of
> doing this?
>
> Thanks
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>