# Has\_many :through failed \<\< returning false

**URL:** <https://www.ruby-forum.com/t/has-many-through-failed-returning-false/91938>\
**Category:** Rails\
**Created:** [April 13, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/has-many-through-failed-returning-false/91938 "2007-04-13T17:02:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [April 13, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/has-many-through-failed-returning-false/91938/1 "2007-04-13T17:02:44Z")

</div>

Hi All,

I’ve got a situation where I want a has\_many :through association to  
behave  
somewhat like a normal has\_many when \<\< is called on the association.  
ie I  
would like it to return false instead of throwing an exception.

With some inspiration from [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com) I’ve come up  
with,

class Container \< AR:B

has\_many :container\_items  
has\_many :items, :through =\> :container\_items do  
def \<\<( item )  
begin  
self.concat item  
rescue ActiveRecord::RecordInvalid =\> e  
@owner.errors.add( :item, e)  
false  
end  
end  
end

Is there anything that stands out as a stark issue with doing this? I  
would  
like to call owner in some other way but I have not found how to get at  
@owner with a method.

The join model has only timestamps in addition to the two f’keys

Any comments, insights, crits etc are welcome.

I’ve extracted this into a plugin if anyone would like to try it. I  
can’t  
make any guarantees though.

[http://svn.devjavu.com/liquid/has\_many\_through\_append\_fix](http://svn.devjavu.com/liquid/has_many_through_append_fix)

Cheers  
Daniel

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [April 14, 2007, 2:15am UTC](https://www.ruby-forum.com/t/has-many-through-failed-returning-false/91938/2 "2007-04-14T02:15:37Z")

</div>

Daniel ----- wrote:

> I’ve got a situation where I want a has\_many :through association to  
> behave  
> somewhat like a normal has\_many when \<\< is called on the association.  
> ie I  
> would like it to return false instead of throwing an exception.
> 
> With some inspiration from [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com) I’ve come up  
> with,
> 
> class Container \< AR:B
> 
> has\_many :container\_items  
> has\_many :items, :through =\> :container\_items do  
> def \<\<( item )  
> begin  
> self.concat item  
> rescue ActiveRecord::RecordInvalid =\> e  
> @owner.errors.add( :item, e)  
> false  
> end  
> end  
> end
> 
> Is there anything that stands out as a stark issue with doing this? I  
> would  
> like to call owner in some other way but I have not found how to get at  
> @owner with a method.

Looks like it might work to me. You can get the association owner in an  
extension with the proxy\_owner method.

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

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [April 20, 2007, 8:33am UTC](https://www.ruby-forum.com/t/has-many-through-failed-returning-false/91938/3 "2007-04-20T08:33:47Z")

</div>

On 4/14/07, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Looks like it might work to me. You can get the association owner in an  
> extension with the proxy\_owner method.
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

Thanx Josh,

I was hopeing you might take a look. I’ll have a go at using an  
extension  
for this when I get home.

Cheers
