I have three tables: Case, Attorney and Firm. Case belongs to Attorney and Attorney Belongs to Firm. I don''t see how I can define that Case belongs to Firm through Attorney since Attorney is not a join table (does not reference both). In order to find all cases for a Firm, it seems I must write a sequence of queries like this: class FirmsController < ApplicationController def show id = params[:id] @attorneys = Attorney.find(:all, :conditions => ["firm_id = ?", id]) @cases = Case.find(:all, :conditions => ["attorney_id = ?", @attorneys.id]) end end In this sequence however, @cases doesn''t return any case objects. How might I get this to work? Thanks, Peter -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Clifford Heath wrote:> 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.Excellent. Thanks Clifford. :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Marks wrote:> I have three tables: Case, Attorney and Firm. Case belongs to Attorney > and Attorney Belongs to Firm. I don''t see how I can define that Case > belongs to Firm through Attorney since Attorney is not a join table > (does not reference both). In order to find all cases for a Firm, it > seems I must write a sequence of queries like this: > > class FirmsController < ApplicationController > > def show > id = params[:id] > @attorneys = Attorney.find(:all, :conditions => ["firm_id = ?", id]) > @cases = Case.find(:all, :conditions => ["attorney_id = ?", > @attorneys.id]) > end > > end > > In this sequence however, @cases doesn''t return any case objects. How > might I get this to work?As others have pointed out, you can use :include to find things. You might also want to use an asymetric join model. Attorney has_many :cases Firm has_many :attorneys has_many :cases, :through => :attorneys @firm = Firm.find(params[:id]) @cases = @firm.cases That will work when the join model is like yours, a belongs_to and a has_many. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Marks wrote: ...>>> @cases = Case.find(:all, :conditions => ["attorney_id = ?", >>> @attorneys.id]) > Excellent. Thanks Clifford. :)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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---