Sergey Alekseev
Sergey Alekseev

Reputation: 12300

Validates presence of each other in two associated models

I have the following two models:

class Parent < ActiveRecord::Base
  has_one :child, dependent: :destroy
  validates :child, presence: true
end

class Child < ActiveRecord::Base
  belongs_to :parent
  validates :parent, presence: true
end

I want to create Parent object.

If I do the following: Parent.create! or Factory(:parent)
Exception raises: ActiveRecord::RecordInvalid: Validation failed: Child can't be blank

But I can't create Child object without Parent object for the same reason - I need to create Parent object first in order to pass presence validation. As it appears I have some kind of infinite recursion here.

How to solve it?

Upvotes: 1

Views: 989

Answers (1)

Siwei
Siwei

Reputation: 21557

UPDATE:

The code below works well in my environment ( Rails3.2.2, ruby 1.8.7)

# parent.rb
class Parent < ActiveRecord::Base
  has_one :child
  validates :child, :presence => true
end
# child.rb
class Child < ActiveRecord::Base
  belongs_to :parent
  validate :parent, :presence => true
end

# parent_test.rb
require 'test_helper'
class ParentTest < ActiveSupport::TestCase
  test "should be saved" do 
    parent = Parent.new(:name => "111")
    child = Child.new(:name => "222", :parent => parent)
    parent.child = child
    parent.save!
    puts "after saved, parent: #{parent.inspect}"
    puts "after saved, child: #{child.inspect}"
    assert parent.id > 0
    assert child.id > 0
  end
end

run this test and got:

Started
after saved, parent: #<Parent id: 980190963, name: "111", created_at: "2012-04-05 23:19:31", updated_at: "2012-04-05 23:19:31">
after saved, child: #<Child id: 980190963, name: "222", parent_id: 980190963, created_at: "2012-04-05 23:19:31", updated_at: "2012-04-05 23:19:31">
.
Finished in 0.172716 seconds.

1 tests, 2 assertions, 0 failures, 0 errors

PREVIOUS ANSWER ================

try to initialize them separately, then add the association, at last save them.

parent = FactoryGirl.build(:parent)
child = FactoryGirl.build(:child, :parent => parent)
parent.child = child

parent.save
child.save  # seems this line of code is redundant?  I am not sure. 

for more details of "build, create", see its official website: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

Upvotes: 2

Related Questions