aniltimt / substruct

Automatically exported from code.google.com/p/substruct
http://code.google.com/p/substruct
6 stars 0 forks source link

PATCH Don't use object ids directly. #104

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Its not reproductible by the web interface.

What version of the product are you using? On what operating system?
Trunk. Ubuntu 7.10

Please provide any additional information below.

Assign an object:
new_order_code = OrderStatusCode.find_by_name("ORDERED - PAID - TO SHIP")
self.order_status_code = new_order_code if new_order_code

instead of just its id:
self.order_status_code_id = 5

when possible.

It behaves wrong unless reloaded and in some situations, (if
you use an object then an id, the relation isn't even saved and doesn't
give any error).

These errors happens when testing the order model alone, and
doesn't happen using the web interface because the controllers
reloads all objects again. (I think that a model method should not rely on
the app flow to do its job and work properly)

The following three tests illustrates the problem (again, I included these
examples in another issue too), very odd.

  # Always use the object and nothing more is needed.
  def test_associate_using_object_all_times
    a = Order.new
    a.order_status_code = order_status_codes(:cart)
    a.save
    assert_equal a.order_status_code, order_status_codes(:cart)
    a.order_status_code = order_status_codes(:to_charge)
    a.save
    assert_equal a.order_status_code, order_status_codes(:to_charge)
  end

  # When using the object then the id, the parent need to be reloaded first
or it will simply ignore the change.
  def test_associate_using_object_then_id
    a = Order.new
    a.order_status_code = order_status_codes(:cart)
    a.save
    assert_equal a.order_status_code, order_status_codes(:cart)
    a.order_status_code_id = 2
    a.save
    assert_not_equal a.order_status_code, order_status_codes(:to_charge)
    a.reload
    a.order_status_code_id = 2
    a.save
    assert_equal a.order_status_code, order_status_codes(:to_charge)
  end

  # When using the id then the object, nothing more is needed.
  def test_associate_using_id_then_object
    a = Order.new
    a.order_status_code_id = 1
    a.save
    assert_equal a.order_status_code, order_status_codes(:cart)
    a.order_status_code = order_status_codes(:to_charge)
    a.save
    assert_equal a.order_status_code, order_status_codes(:to_charge)
  end

Original issue reported on code.google.com by edmundo...@gmail.com on 28 Jun 2008 at 6:35

Attachments:

GoogleCodeExporter commented 9 years ago
Fixed with r115 and verified with new unit tests.

Original comment by subim...@gmail.com on 11 Aug 2008 at 11:16