Rails has_one :through association

  1. Logic, OK it might sound a bit weak for this but it would be logical to say that “I have a supplier who has an account with me, I want to see the entire account history of this supplier”, so it makes sense for me to be able to access account history from supplier directly.

  2. Efficiency, this for me is the main reason I would use :through, simply because this issues a join statement rather than calling supplier, and then account, and then account_history. noticed the number of database calls?

    • using :through, 1 call to get the supplier, 1 call to get account_history (rails automatically uses :join to retrieve through account)

    • using normal association, 1 call to get supplier, 1 call to get account, and 1 call to get account_history

That’s what I think =) hope it helps!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)