I’m trying to find a simple way to reference all of the objects that
belong to their parent’s parent. In other words:
Clients --> Orders --> LineItems
How do I get all of a client’s lineitems in one array (i.e.,
client.all_lineitems)?
I’m assuming LineItem belongs_to :order and Order belongs_to :client.
If you are using Edge Rails, you can set this up as a
has_many…:through association, then you just say
client.orders.line_items and let Rails do the work for you.
If you’re on 1.0, you can do this with some custom SQL:
in class LineItem…
def self.find_for_client(client)
self.find_by_sql(‘SELECT DISTINCT li.* FROM clients c, orders o,
line_items li WHERE c.id = :client_id AND o.client_id = c.id AND
li.order_id = o.id’, :client_id => client.id)
end
client.line_items works.
client.orders.line_items does not work. Your original example was for
client.orders.line_items.
It would be nice to be able to do client.orders.line_items.
D’oh! You’re right, that was a brain fart. client.orders.line_items
won’t work. It would be nice if the association was smart enough to do
that join for you using that syntax, wouldn’t it? I wonder how hard it
would be to add that to the association magic?
client.line_items works.
client.orders.line_items does not work. Your original example was for
client.orders.line_items.
It would be nice to be able to do client.orders.line_items.
It would be even nicer to traverse down to an arbitrary depth:
country = Country.find_by_name(‘United States’)
country.states.clients.orders.line_items # Returns all of the line
items ordered in the United States. This is just an arbitrary example.
The SQL generated would look something like:
SELECT line_items.* FROM states LEFT JOIN clients ON states.id =
clients.state_id LEFT JOIN orders ON clients.id = orders.client_id
LEFT JOIN line_items ON orders.id = line_items.order_id WHERE
states.country_id = 1
-Jonathan
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.