Forum: Ruby on Rails has_many :through failed << returning false

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-04-13 17:02
(Received via mailing list)
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  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

Cheers
Daniel
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2007-04-14 02:15
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  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 Susser
http://blog.hasmanythrough.com
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-04-20 08:33
(Received via mailing list)
On 4/14/07, Josh Susser <rails-mailing-list@andreas-s.net> wrote:
>
>
>
> Looks like it might work to me.  You can get the association owner in an
> extension with the proxy_owner method.
>
> --
> Josh Susser
> 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
This topic is locked and can not be replied to.