getting values from another table...

24 views
Skip to first unread message

Joe Guerra

unread,
Feb 9, 2017, 12:49:54 PM2/9/17
to Ruby on Rails: Talk
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

Colin Law

unread,
Feb 9, 2017, 4:04:25 PM2/9/17
to Ruby on Rails: Talk
On 9 February 2017 at 17:49, Joe Guerra <jgu...@jginfosys.com> wrote:
> 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.

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

Joe Guerra

unread,
Feb 9, 2017, 8:55:58 PM2/9/17
to Ruby on Rails: Talk


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| %>
 
<tr>
 
<td><%= cart.created_at.strftime("%m/%d/%Y") %> | <%= cart.product_id %> </td>
 
<td><%#= link_to 'Show', cart %></td>
 
<td><%#= link_to 'Edit', edit_cart_path(cart) %></td>

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

Hassan Schroeder

unread,
Feb 9, 2017, 10:44:09 PM2/9/17
to rubyonrails-talk
On Thu, Feb 9, 2017 at 5:55 PM, Joe Guerra <jgu...@jginfosys.com> wrote:

> Are you suggesting I nest my product loop in my cart?

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

"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"?

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Joe Guerra

unread,
Feb 10, 2017, 8:55:49 PM2/10/17
to Ruby on Rails: Talk
I"m building a basic cart system, one item per cart.  I'm still trying to learn ruby on rails.

Thanks,
Jo

Colin Law

unread,
Feb 11, 2017, 3:44:16 AM2/11/17
to Ruby on Rails: Talk
On 11 February 2017 at 01:55, Joe Guerra <jgu...@jginfosys.com> wrote:
> I"m building a basic cart system, one item per cart. I'm still trying to
> learn ruby on rails.

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
Reply all
Reply to author
Forward
0 new messages