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 7802865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:23:15+00:00 2026-06-02T01:23:15+00:00

I am following the Agile Web Development book tutorial with some small changes, halfway

  • 0

I am following the Agile Web Development book tutorial with some small changes, halfway through Chapter 12 on Check Out.

I am getting the following error:

NoMethodError (undefined method `price=' for #<LineItem:0x00000103a0de18>):
app/models/cart.rb:11:in `add_deal'
app/controllers/line_items_controller.rb:45:in `create'

Here is my cart model:

class Cart < ActiveRecord::Base
# attr_accessible :title, :body
has_many :line_items, dependent: :destroy

def add_deal(deal_id)
  current_item = line_items.find_by_deal_id(deal_id)
  if current_item
    current_item.quantity += 1
  else
    current_item = line_items.build(deal_id: deal_id)
    current_item.price = current_item.deal.price
  end
current_item
end
def total_price
  line_items.to_a.sum { |item| item.total_price }
end
end

Here is my create action in line_items_controller with the relevant line 45 where it freezes:

def create
  @cart = current_cart
  deal = Deal.find(params[:deal_id])
  @line_item = @cart.add_deal(deal.id)

My line item model:

class LineItem < ActiveRecord::Base
attr_accessible :cart_id, :deal_id, :quantity
belongs_to :order
belongs_to :deal
belongs_to :cart

def total_price
  deal.price * quantity
end
end

Here is my deal model:

class Deal < ActiveRecord::Base
  attr_accessible :description, :expiration, :featured, :image_url, :inventory, :price, :sold, :title, :value, :deal_id

has_many :line_items

before_destroy :ensure_not_referenced_by_any_line_item

validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :title, :description, :image_url, presence: true

private

# ensure that there are no line items referencing this product
def ensure_not_referenced_by_any_line_item
  if line_items.empty?
    return true
  else
    errors.add(:base, 'Line Items present')
    return false
  end
end
end

When I tried using the console, item.deal.price works just fine but not item.price.

In the line_item model, I tried attr_accessible :price but it did fix anything.

I checked my code vs the book and I can’t tell any significant difference at all.

One idea is to set a database field for price for LineItems but the book doesn’t do that and it violates the DRY principle.

Any help would be greatly appreciated as I’ve stared at the source code for hours and can’t find anything wrong. Thanks.

  • 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-02T01:23:16+00:00Added an answer on June 2, 2026 at 1:23 am

    You were distracted during reading the book. LineItem model does contain the price field. This totally complies with DRY principle, as there is a possibility of changing a price of a Product in the future and LineItem model shows as a history of deals.

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

Sidebar

Related Questions

I'm following through the tutorial from the book Agile Web Development with Rails and
Following some code in Agile Web Development book, theres a method to redirect back
I am going through the Agile Web tutorial with some slight changes. When I
Just working through the Agile Web Development with Rails book and near the closing
I'm working through Agile Web Development with Rails, Edition 4 with some tweaks (mostly
I'm following Iteration 13 of Agile Web development, for User login. I create a
I have methodically been working thru the Agile Web Development with Rails book. No
I'm working with the Agile Web Development with Rails (4th edition) book and I
I am following the Agile Web Development tutorials and I came across an annoying
I'm using Instant-Rails 2.0 and following the Depot example project of Agile Web Development

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.