3

accepts_nested_attributes_forで受け入れ中のモデルfind_or_createを試しています。accepts_nested_attributes_forでfind_or_createを試してみませんか?

私はあなたが見ることができるように、各旅行が主催者として知られているGuestUserに属しトリップモデル

class Trip < ActiveRecord::Base 
    attr_accessible :organiser_attributes 

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

を持っています。

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

    has_many :trips, foreign_key: :organiser_id 
end 

は、今私は(検証およびデータベースレベルの両方で)GuestUserの電子メールの一意性制約を持っています。したがって、GuestUserが2回のトリップを作成した場合、2回目のトリップを同じGuestUserレコードに適用したいと思います。

これを実現する方法を説明していると思われるthis questionsが見つかりました。しかし、私はそれを動作させるように見えることはできません。私Tripモデルで

は、私が追加しました:

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 

described in the docsとして。しかし、このテスト:

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 

はメッセージで失敗します。

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 protected]", "name"=>"Peter Pan", "phone"=>"1234543534"}}} 
    (0.0ms) SAVEPOINT active_record_1 
    (0.1ms) SELECT 1 FROM "guest_users" WHERE "guest_users"."email" = '[email protected]' 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" 

ことと次第です何:

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)>' 

ここでテスト・ログはありますか?ところで、私はRails 3.1.3を使用しています。

答えて

1

通常、関係の親であるモデル(つまり、所有の所有クラス)、つまりhas_many:chidrenステートメントを含むモデルには、を入れます。

+0

私はそれも動作を停止しない?つまり、親と子の両方の協会から働くことができます。 –

+0

キーは「ネストされた」という単語です。通常は、親の...ネストにネストされた子を見つけます。 – maprihoda

関連する問題