Hey Aye, Yeah, now that I look back at my site, it is for join tables. I have readers and doors, and they are joined through reader_activities, which has a door_id AND a reader_id. I think you can drop the :through statement, and calling firm.clients.invoices.find should work fine for ya. But try it out and let me know :)
- Adam On Feb 6, 2008 10:49 AM, aye <[EMAIL PROTECTED]> wrote: > > Adam, > It's exactly like you described. I've gone over the relationships over > and over again and I was thinking maybe I was already in the "too-long- > on-an-issue-making-stupid-mistakes" zone. I'm starting to think maybe > that the "through" relationship is meant more for join tables and not > really for parent/child/grandchild tables. > > Client > has_many :invoices > belongs_to :firm > > Invoice > belongs_to :client > > > On Feb 6, 10:43 am, "Adam Grant" <[EMAIL PROTECTED]> wrote: > > Hey Aye, > > > > What do your Invoice and Client model relationships look like? Does > Client > > have > > > > has_many :invoices > > belongs_to :firm > > > > statement in it? Does Invoice have > > > > belongs_to :client > > > > as well? > > > > - Adam > > > > On Feb 6, 2008 10:12 AM, aye <[EMAIL PROTECTED]> wrote: > > > > > > > > > Hey all, > > > First time poster here and just started attending the meets (see you > > > all tomorrow!). The problem I'm seeing is a simple join but the ORM > > > settings are getting me confused. > > > > > I've got a model relationship similar to the firm/client/invoice > > > example in the ActiveRecord::Associations::ClassMethods example. Firm > > > has many Clients and Client has many Invoices. So Client has a foreign > > > key to Firm and Invoice has foreign key to Client. According to the > > > docs I can relate Firm to > > > Invoices using the ":through". > > > > > ---------------------------------- > > > class Firm > > > has_many :clients > > > has_many :invoices, :through => :clients > > > ---------------------------------- > > > > > If I try to do this: > > > @firm.invoices.find(params[:invoice_id]) > > > > > its generating the following SQL: > > > > > ---------------------------------- > > > SELECT > > > invoices.* > > > FROM invoices > > > INNER JOIN clients ON invoices.client_id = clients.invoice_id > > > WHERE (`invoices`.`invoice_id` = 1 AND ((clients.firm_id = 2))) > > > ---------------------------------- > > > > > As you can see the INNER JOIN is screwed up where its somehow thinking > > > that the Client table has a foreign key to the Invoice table. I know > > > that the "has_many/through" is for typical association tables and > > > might not apply to the parent-child-grantchild table structure but > > > since I saw the example in the docs I thought this should work. > > > Any ideas? > > > > > Thanks! > > > Aye > > > --~--~---------~--~----~------------~-------~--~----~ SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby -~----------~----~----~----~------~----~------~--~---
