Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8241429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:50:07+00:00 2026-06-07T20:50:07+00:00

Taking this snippet from the famous agile web development with rails book (most recent

  • 0

Taking this snippet from the famous agile web development with rails book (most recent edition):

def create 

  @cart = current_cart 

  product = Product.find(params[:product_id])

  @line_item = @cart.line_items.build(product: product)

This is for a general ecommerce/depot app, and this function is for the “Add to cart” button for a specific product. Here’s where I’m confused:

I imagined the code being:

@line_item = line_items.build(product: product) 

@line_item = line_items.build(cart: @cart) 

Basically If a line_item belongs to both a product and a cart, I create those relationships separately.

Also, if the cart doesn’t have any line_items yet, then how can I do @cart.line_items? I understand line_items.build(product: product) will return to me a line_item object (which I save into @line_item), but how does cart.line_items work if there’s no line_items in the cart yet?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:50:10+00:00Added an answer on June 7, 2026 at 8:50 pm

    In the Cart model, there is most likely a has_many association with the LineItem model called line_items. Rails will automatically initialize that association with an empty array.

    You can think of @cart being initialized as:

    @cart = Cart.new
    @cart.line_items = []
    

    Every time you add call @cart.line_items.build, think of it as this:

    new_line_item = LineItem.new cart: @cart
    @cart.line_items.push new_line_item
    

    Note: This is absolutely not how the actual code works, but you can think of the functional behaviour as the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Taking this file as an example, I'm trying to read the data in a
I want to process every element of a for-loop. Taking this code, why is
Is it possible to somehow do this without taking out the @? It does
How to implement a gmail-like this is taking too long warning message using jQuery
Refreshing on floating points (also PDF ), IEEE-754 and taking part in this discussion
After taking a look at this SO question and doing my own research, it
This problem is taking me too long to fix I could use some guidance
This bit of code is taking almost a half second to execute. Could somebody
Taking a JavaScript object with 4 properties: function Object() { this.prop1; this.prop2; this.prop3; this.prop4;
I have this query which is a dependant query and taking much execution time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.