getting values from another table...

I’ve got a simple cart table with a link to a product id.

I’d like to grab the values from the product table that match the product id in the cart, but I’m looping thru the cart in an index view.

So, I wasn’t quite sure how to accomplish this.

any suggestions?

Thanks,

Joe

Assuming that you have Cart has_many products, and if you haven't then
you probably should have, then you can say (if the cart is in @cart)

@cart.products.each do |product|
  # at this point product contains the Product
end

Colin

hmm, ok. I’ll add the has many products to my cart.rb file. Here is my current cart index.
Are you suggesting I nest my product loop in my cart?

Thanks,

Joe

<% @carts.each do |cart| %>

<%= cart.created_at.strftime("%m/%d/%Y") %> | <%= cart.product_id %> <%#= link_to 'Show', cart %> <%#= link_to 'Edit', edit_cart_path(cart) %>

<% if not cart.processing? %>

| <%= link_to 'Remove', cart, method: :delete, data: { confirm: 'Are you sure?' } %> <% else %> <%= '| Processing order...' %> <% end %> <% end %>

``


"carts" plural is an unusual pattern - a single cart with multiple items
is more typical, and to my mind maps more closely to the physical
world's idea of a "shopping cart".

What's the benefit of multiple "carts"?

I"m building a basic cart system, one item per cart. I’m still trying to learn ruby on rails.

Thanks,

Jo

In that case you want Cart belongs_to product and Product has_many
carts (the verb has_many can be confusing, it really means
is_associated_with_many), then to get the product for a cart you use
cart.product in your carts loop. Eventually when the cart can have
many products you can use Cart has_many_and_belongs_to product, and
vice versa, but I usually recommend using an explicit join table
rather the the automatic table that has_many_and_belongs_to generates.
I am sure that was covered in railstutorial.org.

Colin