Topic: Simple question about models

Hi all,

I have a real simple question to ask you guys.

I have 2 models: Clients & Projects.
Here is how I defined both models in RoR:

class Client < ActiveRecord::Base
  has_many  :projects;
  validates_presence_of :name;
  validates_uniqueness_of :name;
end

class Project < ActiveRecord::Base
  belongs_to  :client;
  validates_presence_of :client_id, :name;
end

Now i want to be able to check how many projects I have for every client.
So I have created a function inside the Project model that just just that:

  def self.CountTotalProjects(client)
    count(:all, :conditions => ['client_id = ?', client.id])
  end

But it doesn't work sad

Can anybody help me?

Another question. How can I display the list of projects for a specific client in his "Show" section?

Thanks.

Re: Simple question about models

Well first off, you don't need those yucky semicolons after your validations and what not.

And as for your main problem...
You might want to make that method as part of the Client model instead.  It makes more sense to have:

Client.count_projects

than
Project.CountTotalProjects(@client)

And to display that list of projects for a client:

In your view:

<%= render :partial => 'project', :collection => @client.projects %>

(Assuming your relationship between Clients and Projects is many to many or one to many)
The :collection part of the render_partial just tells rails to go through all of the contained variables (your client's projects) one by one and run them through the 'project' partial.