Forum: Ruby on Rails :through causes White Screen of Death

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.
Ac31d0d6453b50ad6fdfe16742273424?d=identicon&s=25 unknown (Guest)
on 2006-04-03 12:33
(Received via mailing list)
i'm trying to get :through associations working per the wiki:
http://wiki.rubyonrails.com/rails/pages/ThroughAssociations

i follow the instruction to the t, with the exception that i'm trying to
use it
with a polymorphic association. here are the associations in my models:

Collection has_many :collectings
Collection has_many :collectables, :through => collectings

Collecting belongs_to :collection
Collecting belongs_to :collectable, :polymorphic => true

Book has_many :collectings, :as => :collectable
Book has_many :collections, :through => :collectings

i'm able to access collectables like this:
@collection.collectings.collect{|collecting| collecting.collectable}

but when i try and access them using the :through, like this:
@collection.collectables

i get the White Screen of Death (which is supposed to be banished from
1.1!)

fwiw, i need the association to be polymorphic because in my app,
Magazines are
also collectable.

if anyone has any ideas, i'd be grateful. i can use the workaround, but
i'm
baffled why :through isn't working for me.

best,
john



_____________________

John McGrath
http://fryolator.com

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-04-03 18:48
unknown wrote:
> i'm trying to get :through associations working per the wiki:
> http://wiki.rubyonrails.com/rails/pages/ThroughAssociations
>
> i follow the instruction to the t, with the exception that i'm trying to use it
> with a polymorphic association. here are the associations in my models:
>
> Collection has_many :collectings
> Collection has_many :collectables, :through => collectings
>
> Collecting belongs_to :collection
> Collecting belongs_to :collectable, :polymorphic => true
>
> Book has_many :collectings, :as => :collectable
> Book has_many :collections, :through => :collectings
>
> i'm able to access collectables like this:
> @collection.collectings.collect{|collecting| collecting.collectable}
>
> but when i try and access them using the :through, like this:
> @collection.collectables
>
> i get the White Screen of Death (which is supposed to be banished from 1.1!)
>
> fwiw, i need the association to be polymorphic because in my app,
> Magazines are also collectable.
>
> if anyone has any ideas, i'd be grateful. i can use the workaround, but
> i'm baffled why :through isn't working for me.

Unfortunately you can't do what you are attempting. I ran into the same
problem myself, and there's no way around it. If you dig into how
polymorphic associations work, you discover that the reference is
essentially a composite foreign key. That key consists of the x_id and
x_type fields. The x_id field holds the primary key of the associated
object's record in its table, and the x_type field holds the Ruby class
name of the associated object. Notice that nowhere in there is any
information to locate the table of the associated object.

Polymorphic has_many :through associations are supported, but you can
only traverse them in one direction. Unless you add some additional
constraints to let Rails find the record in its table. Try something
like this in your Collection class:

  has_many :books, :through => collectings, :source => :collectables,
                        :conditions => "collectings.collectable_type =
'Book'"
  has_many :magazines, :through => collectings, :source =>
:collectables,
                        :conditions => "collectings.collectable_type =
'Magazine'"

If you really need a polymorphic array of all the Collectables, you'll
have to simulate the join in Ruby.

def all_collectables
  self.collectings.collect { |c| c.collectable }
end

--
Josh Susser
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.