title: Newbie: Rails' way to query database in my case
body: <p>I am using <strong>Ruby v1.8</strong> and <strong>Rails v2.3</strong>.</p>
  
  <p>I have a two model objects: <strong>Cars</strong> and <strong>Customers</strong>,</p>
  
  <p>Model <strong>Cars</strong>:</p>
  
  <pre><code>class car &lt; ActiveRecord::Base
  
    #car has attribute :town_code
  
    has_many :customers
  
  end
  </code></pre>
  
  <p>Model <strong>Customers</strong>:</p>
  
  <pre><code>class customer &lt; ActiveRecord::Base
    # customer has attribute :first_name, :last_name
    belongs_to :car
  end
  </code></pre>
  
  <p>Now in my controller, I got a string from VIEW, and the received string has the format <strong>firstname.lastname@town_code</strong>, for example a string like "<strong>John.smith@ac01</strong>" which can be parsed as <code>first_name="John"</code>, <code>last_name="smith"</code> and <code>town_code="ac01"</code></p>
  
  <p>Now I would like use the <strong>Rails's way to query the database</strong> to find all the customer objects (match the above conditions) from Customers table which has :</p>
  
  <hr>
  
  <p><strong>first_name="John"</strong>,</p>
  
  <p><strong>last_name="smith"</strong></p>
  
  <p>and owned a car(by car_id) with car's <strong>town_code="ac01"</strong>. </p>
  
  <hr>
  
  <p><strong>what is Rails' syntax to query this?</strong> </p>
  
  <p>I know it should be something like (if I wanna count the nr of matched customer): </p>
  
  <p><code>Customer.count :consitions =&gt;{:first_name =&gt; "John", :last_name=&gt;"smith"...}</code> </p>
  
  <p><strong>But, I am not sure how to refer to a customer that has a referenced car with car's</strong> <code>town_code= "ac01"</code> <strong>?</strong></p>
  
  <p><strong>------------------   My question      --------------------</strong></p>
  
  <p>I want to have <strong>two queries</strong>: </p>
  
  <p>-one is used to count the number of matching customers, </p>
  
  <p>-the other query returns the customers objects like <code>find_by_</code> query. </p>
  
  <p><strong>What is the syntax in Ruby on Rails for the two queries?</strong></p>