:through causes White Screen of Death


#1

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.


#2

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 S.
http://blog.hasmanythrough.com