SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problem updating child row from parent model

    I developped a web site with Ruby on Rails, but I get a problem during the testing.
    I have a parent table called "orders" and a child table called "line_items".
    I created some data for the test in the files "test/fixtures/orders.yml" and "test/fixtures/line_items.yml".
    In the file "test/unit/order_test.rb" I wrote some code to test the "Add", "Update" and "Destroy" for my model"Order".
    My problem is that on the "Update" I try to update a quantity which is a field of my table "line_items" which is a child of my table "orders".

    For that test I used the following code:

    def test_update
    assert_equal 2, @order.line_items.first.quantity
    @order.line_items.first.quantity = 4
    assert @order.save, @order.errors.full_messages.join("; ")
    @order.reload
    assert_equal 4, @order.line_items.first.quantity
    end

    And I get the following result:

    1> Failure:
    test_update<OrderTest> [test/unit/order_test.rb:25]:
    <4> expected but was <2>.

    I tried lots of different things, but I always get that error. I really don't know why.

    Here's the declaration of my model order.rb

    class Order < ActiveRecord::Base
    has_many :line_items, :exclusively_dependent => true
    end

    Here's the declaration of my model line_item.rb

    class LineItem < ActiveRecord::Base
    belongs_to :order
    end

    And I put at the begining of the order_test.rb file.

    fixtures :orders, :line_items

    I guess I'm doing wrong, maybe it's not the way to update a child row or I probably miss something, unless we can't update a child row through a parent model.
    If someone has the answer and can help me to figure out why the update doesn't work, that will be great.

    Thank you.
    Last edited by sweatje; Jun 6, 2006 at 07:43. Reason: disable smilies to make code readable

  2. #2
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing assert @order.save ....
    to assert @order.save! ...
    This will throw an exception to show you why you're not updating.

  3. #3
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help.

    I tried it, but the error message is still the same.
    I really don't know why it does work and how can I update my child and parent tables at the same time in one statement.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •