Forum: Ruby on Rails Polymorphic Associations

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.
Dylan S. (Guest)
on 2006-02-09 02: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
Rick O. (Guest)
on 2006-02-09 05:14
(Received via mailing list)
On 2/8/06, Dylan S. <removed_email_address@domain.invalid> 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 O.
http://techno-weenie.net
Dylan S. (Guest)
on 2006-02-09 07: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.)
This topic is locked and can not be replied to.