3

I'm trying to find_or_create an associated model which is being accepted via accepts_nested_attributes_for.

I have a Trip model

class Trip < ActiveRecord::Base
  attr_accessible :organiser_attributes

  accepts_nested_attributes_for :organiser
  belongs_to :organiser, class_name: 'GuestUser', :autosave => true
end

As you can see, each trip belongs to a GuestUser who is known as the organiser.

class GuestUser < ActiveRecord::Base
  attr_accessible :name, :email, :phone

  has_many :trips, foreign_key: :organiser_id
end

Now I have a uniqueness constraint on the email of GuestUser (at both the validation and database level). So, if a GuestUser creates 2 trips, I would like the second trip to be applied to the same GuestUser record.

I found this questions which seems to describe the way to accomplish this. However, I can't seem to make it work.

In my Trip model I have added:

class Trip < ActiveRecord::Base
  #other stuff ...

  belongs_to :organiser, class_name: 'GuestUser', autosave: true

  def autosave_associated_records_for_organiser
    # Find or create the organiser by name
    if new_organiser = GuestUser.find_by_email(organiser.email) then
      self.organiser = new_organiser
    else
      self.organiser.save!
    end
  end
end

as described in the docs. But this test:

describe TripsController do
  describe "POST create success" do
    before :each do
      @guest_user = Factory.attributes_for(:guest_user)
      @trip = Factory.attributes_for(:trip)
      @valid_attr = @trip.merge(organiser_attributes: @guest_user)
    end

    describe "if the guest user already exists" do
      it "should still create a trip" do
        GuestUser.create! @guest_user
        expect do
          post :create, trip: @valid_attr, format: :json
        end.to change(Trip, :count).by(1)
      end
    end
  end
end

fails with the message:

Failures:
  1) TripsController POST create success if the guset user already exists should still create a trip
      Failure/Error: expect do
        count should have been changed by 1, but was changed by 0
      # ./spec/controllers/trips_controller_spec.rb:26:in `block (4 levels) in <top (required)>'

Here is the test log:

  Processing by TripsController#create as JSON
  Parameters: {"trip"=>{"price"=>"3456", "origin_departure_time"=>"2011-12-13 18:08:15 +0000", "destination_arrival_time"=>"2011-12-13 20:08:15 +0000", "destination_departure_time"=>"2011-12-13 23:08:15 +0000", "origin_arrival_time"=>"2011-12-13 22:08:15 +0000", "organiser_attributes"=>{"email"=>"email@example.com", "name"=>"Peter Pan", "phone"=>"1234543534"}}}
   (0.0ms)  SAVEPOINT active_record_1
   (0.1ms)  SELECT 1 FROM "guest_users" WHERE "guest_users"."email" = 'email@example.com' LIMIT 1
   (0.0ms)  ROLLBACK TO SAVEPOINT active_record_1
Completed 422 Unprocessable Entity in 6ms (Views: 0.3ms | ActiveRecord: 0.1ms)
   (0.0ms)  SELECT COUNT(*) FROM "trips"

What's up with that? I'm using Rails 3.1.3 by the way.

Community
  • 1
  • 1
David Tuite
  • 22,258
  • 25
  • 106
  • 176

1 Answers1

1

Normally, you put accepts_nested_attributes_for in the model that is the parent of the relationship (aka owning class of the association), ie the model that contains the has_many :chidren statement.

Marek Příhoda
  • 11,108
  • 3
  • 39
  • 53