Deeply nested associations

53 views
Skip to first unread message

Michael Moulsdale

unread,
May 28, 2013, 12:11:14 AM5/28/13
to fabrica...@googlegroups.com
I'm trying to create nested associations using the following code

    it "builds multiple nested associations" do
        equipment_1 = Fabricate(:equipment)
        customer_1 = Fabricate(:customer)
        customer_2 = Fabricate(:customer)
        trip_1 = Fabricate(:trip, date: Date.today) do
            participations { [Fabricate(:participation, customer_id: customer_1.id )] } do
                rentals { [Fabricate(:rental, equipment_id: equipment_1.id )] }
            end
        end
        expect(trip_1).to be_valid
    end

where trip has_many participations, and participation has_many rentals

However I get an error stating unexpected keyword_do_block

Is there a way of creating deeply nested associations?

Michael

Michael Moulsdale

unread,
May 28, 2013, 5:22:16 AM5/28/13
to fabrica...@googlegroups.com
Sorry, wasn't thinking straight, and got my brackets in the wrong places. Got that sorted out with the following method. Works a treat

    it "builds multiple associations" do

        customer_1 = Fabricate(:customer)
        customer_2 = Fabricate(:customer)
        equipment = Fabricate(:equipment)

        trip_1 = Fabricate(:trip, date: Date.today) do
            participations(count: 2) { Fabricate.build(:participation, customer_id: customer_1.id) do
                rentals(count: 1) { Fabricate.build(:rental, equipment_id: equipment.id) }
            end }
        end
        expect(Participation.where("trip_id =?", trip_1.id).size).to eq 2
    end

Reply all
Reply to author
Forward
0 new messages