Ruby Forum Ruby on Rails > Polymorphic Associations

Posted by Dylan Stamat (Guest)
on 09.02.2006 01:00
(Received via mailing list)
If anybody on edge familiar with this could help, that would be "great" 
:)

I have different types of things I want to be "reviewable".  So, instead 
of
having a slew of HABTM... this new Polymorphic Associations schtick 
seemed
like the best solution.  I have everything setup as I thought it should 
be
(so I think)... but it doesn't appear to be working correctly.  Here is 
my
setup:


User        (table Clients:  basic user stuff belonging to a functional 
STI
relationship)
==========
def User < Client
   has_many  :reviewings, :as => :reviewable, :dependent => true
   has_many  :reviews, :through => :reviewings
end
==========

Review     (table Reviews: id, rating, review)
==========
def Review < AR:B
  has_many  :reviewings
end
==========

Reviewing  (table Reviewings:  id, review_id, reviewable_id,
reviewable_type)
==========
def Reviewing < AR:B
  belongs_to :review
  belongs_to :reviewable, :polymorphic => true
end
==========


Now, within console:
===========
>> u = User.find(:first)
my user stuff
>> r = Review.find(:first)
my review stuff
>> u.reviews << r
added r to u's reviews

I check the "reviews" table, and the review is successfully entered...
however, there is no entry in "reviewables".
I've played around with this for quite a while trying to debug why an 
insert
isn't happening into this table, but no dice.

Any insight into this would be greatly appreciated !
==
Dylan
Posted by Rick Olson (Guest)
on 09.02.2006 04:14
(Received via mailing list)
On 2/8/06, Dylan Stamat <dylans@gmail.com> wrote:
> relationship)
>   has_many  :reviewings
> ==========
>
> I check the "reviews" table, and the review is successfully entered...
> however, there is no entry in "reviewables".
> I've played around with this for quite a while trying to debug why an insert
> isn't happening into this table, but no dice.
>
> Any insight into this would be greatly appreciated !
> ==
> Dylan

do you have a reviewable_id and reviewable_type field?  polymorphic
joins are like backwards STI classes...

--
Rick Olson
http://techno-weenie.net
Posted by Dylan Stamat (Guest)
on 09.02.2006 06:02
(Received via mailing list)
Yup !  It's in the "reviewings" table.  (note the column names next to 
the
class definitions in my first email.)