# Validating both sides of a has\_one relationship breaks pickle/machinist tests

**URL:** <https://www.ruby-forum.com/t/validating-both-sides-of-a-has-one-relationship-breaks-pickle-machinist-tests/182706>\
**Category:** Rails\
**Created:** [February 1, 2010, 7:00pm UTC](https://www.ruby-forum.com/t/validating-both-sides-of-a-has-one-relationship-breaks-pickle-machinist-tests/182706 "2010-02-01T19:00:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [February 1, 2010, 7:00pm UTC](https://www.ruby-forum.com/t/validating-both-sides-of-a-has-one-relationship-breaks-pickle-machinist-tests/182706/1 "2010-02-01T19:00:41Z")

</div>

A lot has been posted about validation issues with associations,  
however, I don’t see this issue addressed specifically.

ex:  
class Foo  
has\_one :schedule, :dependent =\> :destroy  
validates\_presence\_of :schedule

class Schedule  
belongs\_to :foo  
validates\_presence\_of :foo\_id

this creates a circular dependency that breaks test frameworks like  
pickle and machinist.  
At first I was surprised a little that you can contsruct objects with  
this constraint  
…you can of course with “new” and “save”  
…though I it sounds like the destroy will cause a problem.  
[http://mohammed.morsi.org/blog/taxonomy/term/29](http://mohammed.morsi.org/blog/taxonomy/term/29)

is there a workaround for tests?

or is this a bad idea from the start?

Nevertheless, It seems to me that while a noble goal – to validate  
both sides of the assoication.  
How else to do it?  
thanks?

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [February 1, 2010, 7:03pm UTC](https://www.ruby-forum.com/t/validating-both-sides-of-a-has-one-relationship-breaks-pickle-machinist-tests/182706/2 "2010-02-01T19:03:19Z")

</div>

Doug wrote:

> A lot has been posted about validation issues with associations,  
> however, I don’t see this issue addressed specifically.
> 
> ex:  
> class Foo  
> has\_one :schedule, :dependent =\> :destroy  
> validates\_presence\_of :schedule
> 
> class Schedule  
> belongs\_to :foo  
> validates\_presence\_of :foo\_id
> 
> this creates a circular dependency that breaks test frameworks like  
> pickle and machinist.  
> At first I was surprised a little that you can contsruct objects with  
> this constraint  
> …you can of course with “new” and “save”  
> …though I it sounds like the destroy will cause a problem.  
> [http://mohammed.morsi.org/blog/taxonomy/term/29](http://mohammed.morsi.org/blog/taxonomy/term/29)
> 
> is there a workaround for tests?
> 
> or is this a bad idea from the start?

It’s a bad idea from the start. Get rid of the validation on the  
has\_one side.

> Nevertheless, It seems to me that while a noble goal – to validate  
> both sides of the assoication.  
> How else to do it?

Don’t. It’s unnecessary.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

> thanks?

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [February 1, 2010, 7:46pm UTC](https://www.ruby-forum.com/t/validating-both-sides-of-a-has-one-relationship-breaks-pickle-machinist-tests/182706/3 "2010-02-01T19:46:23Z")

</div>

thanks for the quick response…some follow up questions:

1. why get rid of the validation on the has\_one side rather than the  
belongs\_to?

2. why do you call it unnecessary?

the requirement that Foo has a schedule (in my example) and vice-versa  
seems reasonable.  
However, given the above, I don’t see how that works with validations.

How would we satisfy that requirement w/o validations?  
perhaps a factory method?  
or using a callback on create?  
where we create a default schedule.

thinking out loud here…

thanks again.

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [February 1, 2010, 8:05pm UTC](https://www.ruby-forum.com/t/validating-both-sides-of-a-has-one-relationship-breaks-pickle-machinist-tests/182706/4 "2010-02-01T20:05:48Z")

</div>

Doug wrote:

> thanks for the quick response…some follow up questions:
> 
> 1. why get rid of the validation on the has\_one side rather than the  
> belongs\_to?

On the belongs\_to side, you can check for a foreign key within the  
object, which is straightforward. There’s no simple way to do it from  
the has\_one side.

> 1. why do you call it unnecessary?

Because it serves no actual purpose. It does not gain you any  
additional functionality or validation.

> the requirement that Foo has a schedule (in my example) and vice-versa  
> seems reasonable.  
> However, given the above, I don’t see how that works with validations.
> 
> How would we satisfy that requirement w/o validations?

Since a Foo is only ever going to have one Schedule, you can create the  
schedule in Foo.new or before\_save, or when Foo#schedule is first  
called.

In general, you only need validation on the belongs\_to side.

> perhaps a factory method?  
> or using a callback on create?  
> where we create a default schedule.

Yes, a callback is a good approach. Also consider merging the two  
tables – has\_one is a bit smelly.

> thinking out loud here…
> 
> thanks again.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
