25 Nov 2012, 18:00

Álvaro González (2 posts)

Hi there,

I’ve just completed D3 and there’s something that is bugging me. I am new with rails, so forget me if the question is obvious.

In the line items controller, while creating a new line item, we wrote this line:

@cart = current_cart product = Product.find(params[:product_id]) @line_item = @cart.line_items.build(:product => product))

My question is, why do we need to pass the whole product as a parameter? Couldn’t we just pass the :product_id)? Wouldn’t that be more efficient?

I can’t see the constructor of line_item (CoC), so I guess that as a line_item belongs to a product and a cart, if we use the default “build” method we need to pass both of them. Why haven’t we modified the code to pass instead the :cart_id and the :product_id?

Thanks, regards

14 Dec 2012, 04:49

Adrian Lee Elder (9 posts)


I’m working my way through the book as well, just note that the third line of your code changes as the book goes on. Good luck!

23 Mar 2013, 11:55

Sam Ruby (584 posts)

There is no difference in performance between passing an ActiveRecord or an Fixnum (Ruby’s integer class).

This book does expose you to both styles. Choose the one that you feel most comfortable with!

  You must be logged in to comment