Querying an unrelated table

Peter Marks wrote:

  def show
    id = params[:id]
    @attorneys = Attorney.find(:all, :conditions => ["firm_id = ?", id])
    @cases = Case.find(:all, :conditions => ["attorney_id = ?",
@attorneys.id])
  end

@cases = Case.find(:all,
    :include => [ { :attorney => :firm } ],
    :conditions => [ "firm.id = ?", params[:id]])

Clifford Heath.

Peter Marks wrote:
...

    @cases = Case.find(:all, :conditions => ["attorney_id = ?",
@attorneys.id])

Excellent. Thanks Clifford. :slight_smile:

BTW, @attorneys.id isn't what you think, in your original code.
You got the Ruby object_id, not the id attribute from ActiveRecord.
This is a major trap, because it sometimes does what you want.

Just ALWAYS write @thing[:id] instead... or build your schemas with
config.active_record.primary_key_prefix_type = :table_name
and suffer the fact that the generators might not all do the right
thing.

Clifford Heath