This is probably a very simple question, I apologise - I'm new to rails.

I've got 2 controllers - customers and orders. I've got it so that when a customer places an order, their id is passed in a hidden field, so that the customer_id field in the orders table has their id. So all orders have the id of the customer who placed it. What I'm trying to do is have the show view for the customers display all their orders - so all the orders with their id.

I've got the customer has_many orders and orders belong_to customers etc. How do I reference the customers/orders to extract the right info? And do I need to put extra info in the show action on the controller? I've tried everything I can think of! So far, I've been able to get a hash of all the info to appear in the show view, but I can't get individual bits of info to appear - e.g. order.price.

I basically want a table of the order details in the customers show view - order price, date placed etc. Any help would be much appreciated, thanks!

Edit: I now have this - but can't get the line_items bit to work. The relationships work like this: customer has many orders, orders have many line items, line items belong to products. I suspect the reason it's not working is because of the belongs_to.

Thanks! That's it! Also, if I wanted to display individual items from the order (using the line_items controller I've got), would I just put "has_many :line_items, through: :orders" in the customer model, then reference it as order.line_item.name?
–
ecsJun 18 '12 at 15:23

1

You probably don't want to put a has_many through on the customer for line_items, because the line_items only make sense in the context of their order (has_many through would make the customer have a big bucket of line_items independent of orders). If order has_many line_items, then, as you're iterating through orders (as above) and have a given order, you can do order.line_items.each do |line_item| and iterate through those as a loop within a loop.
–
Jacob MattisonJun 18 '12 at 15:27

Please can you post the code for a nested loop - I've tried various ways of doing it and I'm getting error messages! Thanks so much!
–
ecsJun 18 '12 at 15:33

I'm now having to nest a loop in a loop in a loop - is that even possible!? I'm getting bogged down now, as the customer has many orders, and the order has many line items. The line items belong to products - how do I reference this in the code!? I've got the order.line_items.each, then how would I go to the products? line_item.products.each isn't working :S thanks!
–
ecsJun 18 '12 at 15:46