# Has\_one triggers save!?

**URL:** <https://www.ruby-forum.com/t/has-one-triggers-save/165265>\
**Category:** Rails\
**Created:** [April 11, 2009, 2:07am UTC](https://www.ruby-forum.com/t/has-one-triggers-save/165265 "2009-04-11T02:07:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kojilab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8c91f0/32.png) [@kojilab](https://www.ruby-forum.com/u/kojilab)\
**Post date:** [April 11, 2009, 2:07am UTC](https://www.ruby-forum.com/t/has-one-triggers-save/165265/1 "2009-04-11T02:07:38Z")

</div>

Hi,

I have a weird behavior where an object association triggers a save  
when the parent record is not new.

My association is polymorphic like this

class UserGuideline \< ActiveRecord::Base  
has\_one :recurring\_task, :class\_name =\> ‘UserRecurringTask’, :as  
=\> :taskable, :dependent =\> :destroy  
end

class UserRecurringTask \< ActiveRecord::Base  
belongs\_to :taskable, :polymorphic =\> true  
end

If do  
g=UserGuideline.find(id)  
g.recurring\_task = UserRecurringTask.new

it triggers the save and the validation

Anyone had this issue?

Thanks

---

<div class="post-metadata">

**Author:** ![kojilab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8c91f0/32.png) [@kojilab](https://www.ruby-forum.com/u/kojilab)\
**Post date:** [April 11, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/has-one-triggers-save/165265/2 "2009-04-11T17:07:50Z")

</div>

On 11 Apr 2009, at 01:06, kojilab wrote:

> Hi,
> 
> I have a weird behavior where an object association triggers a save  
> when the parent record is not new.

According to “Unsaved objects and association” at

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

“Assigning an object to a has\_one association automatically saves that  
object and the object being replaced (if there is one),  
in order to update their primary keys - except if the parent object  
is unsaved (new\_record? == true).”

Fred
