# How to override append method for related attribute?

**URL:** <https://www.ruby-forum.com/t/how-to-override-append-method-for-related-attribute/101273>\
**Category:** Rails\
**Created:** [August 3, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/how-to-override-append-method-for-related-attribute/101273 "2006-08-03T23:18:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_SSFischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@Michael\_SSFischer](https://www.ruby-forum.com/u/Michael_SSFischer)\
**Post date:** [August 3, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/how-to-override-append-method-for-related-attribute/101273/1 "2006-08-03T23:18:21Z")

</div>

I have a model that looks something like this:

class Person \< ActiveRecord::Base  
has\_many :tasks  
[…]  
end

I’d like to override/overload the append (\<\<) method for the tasks  
relational attribute such that I can perform some error checking in  
the context of a Person.tasks \<\< task operation. Is there any way to  
do this?

I wish I could do this in the context of validation, but it appears  
that appending a task instance to a list associated with a person  
instance causes the model to be saved in the database even if doing  
the operation would cause validation to fail. i.e., person.valid?  
will return false as one would expect, but the data will persist!

I’d love your input on this matter.

–Michael

---

<div class="post-metadata">

**Author:** ![Michael\_SSFischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@Michael\_SSFischer](https://www.ruby-forum.com/u/Michael_SSFischer)\
**Post date:** [August 5, 2006, 3:37am UTC](https://www.ruby-forum.com/t/how-to-override-append-method-for-related-attribute/101273/2 "2006-08-05T03:37:47Z")

</div>

Michael S. Fischer wrote:

> do this?
> 
> I wish I could do this in the context of validation, but it appears  
> that appending a task instance to a list associated with a person  
> instance causes the model to be saved in the database even if doing  
> the operation would cause validation to fail. i.e., person.valid?  
> will return false as one would expect, but the data will persist!

Invalid records will persist in the loaded collection array, but  
will not be saved to the database. Are you sure the invalid  
object is getting saved to the DB?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Michael\_SSFischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@Michael\_SSFischer](https://www.ruby-forum.com/u/Michael_SSFischer)\
**Post date:** [August 5, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/how-to-override-append-method-for-related-attribute/101273/3 "2006-08-05T19:40:27Z")

</div>

Michael S. Fischer wrote:

> I have a model that looks something like this:
> 
> class Person \< ActiveRecord::Base  
> has\_many :tasks  
> […]  
> end
> 
> I’d like to override/overload the append (\<\<) method for the tasks  
> relational attribute such that I can perform some error checking in  
> the context of a Person.tasks \<\< task operation. Is there any way to  
> do this?

You can do that with an association extension:

has\_many :tasks do  
def \<\<(task)  
if task.should\_be\_appended  
concat(task) # aliased to \<\< in base association proxy  
else  
raise SomeException  
end  
end  
end

> I wish I could do this in the context of validation, but it appears  
> that appending a task instance to a list associated with a person  
> instance causes the model to be saved in the database even if doing  
> the operation would cause validation to fail. i.e., person.valid?  
> will return false as one would expect, but the data will persist!

If the appended object fails validation then the \<\< won’t succeed. Can  
you put the validation test in Task instead of in Person?

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![Michael\_SSFischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@Michael\_SSFischer](https://www.ruby-forum.com/u/Michael_SSFischer)\
**Post date:** [August 8, 2006, 2:49am UTC](https://www.ruby-forum.com/t/how-to-override-append-method-for-related-attribute/101273/4 "2006-08-08T02:49:38Z")

</div>

On 8/5/06, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> concat(task) # aliased to << in base association proxy
> else
> raise SomeException
> end
> end
> 
> ```
> 
> end

Sexy, thanks!

> > I wish I could do this in the context of validation, but it appears  
> > that appending a task instance to a list associated with a person  
> > instance causes the model to be saved in the database even if doing  
> > the operation would cause validation to fail. i.e., person.valid?  
> > will return false as one would expect, but the data will persist!
> 
> If the appended object fails validation then the \<\< won’t succeed. Can  
> you put the validation test in Task instead of in Person?

Indeed, moving the test to Task did the trick. Thanks!

–Michael
