22 Feb 2012, 18:33
Foto criada em 2010-06-17 às 00.19_pragsmall

Daniel Freire (3 posts)

Hey guys,

I was the code written at page 108, and there was a method called “ensure_not_referenced_by_any_line_item”. The first line inside the method is that: “if line_items.empty?”. My question is: Where the attribute “line_items” is declared? What does he holds?

Thank you!

( My english is very bad, sorry. =P )

22 Feb 2012, 18:34
Foto criada em 2010-06-17 às 00.19_pragsmall

Daniel Freire (3 posts)

class Product < ActiveRecord::Base has_many :line_items

before_destroy :ensure_not_referenced_by_any_line_item

validates :title, :description, :image_url, presence: true validates :price, numericality: {greater_than_or_equal_to: 0.01} validates :title, uniqueness: true validates :image_url, allow_blank: true, format: { with: %r{.(gif|jpg|png)$}i, message: ‘must be a URL for GIF, JPG or PNG image’ }

private

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

25 Feb 2012, 02:00
Samr_small_pragsmall

Sam Ruby (584 posts)

The Line is created in section D2.

The “has_many :line_items” in the code you posted above has the effect of declaring an attribute which holds a collection of LineItem records. Similarly, the line “belongs_to :product” in the LitemItem class has the effect of declaring an attribute that contains a reference to a single Product.

04 Mar 2013, 00:39
Photo 22-02-12 3 02 49 p.m._pragsmall

Martin Miranda (3 posts)

Hello, I’ve got the following error when running functional test:

  1) Error:
test_should_destroy_product(ProductsControllerTest):
NameError: undefined local variable or method `lines_items' 
for #<Product:0x007fbb91be03d0>

Any idea? Thanks!

  You must be logged in to comment