As my knowledge, it should be :

class Asset < ActiveRecord::Base
   belongs_to :userable, :polymorphic => true

   def userable_type=(sType)
       super(sType.to_s.classify.constantize.base_class.to_s)
    end
end

class Sound < Asset
end

class User < ActiveRecord::Base
  has_many :assets , :as=>:userable
end

and in your assets table you must have 2 columns :  userable_id and
userable_type

best regards


On Sep 19, 1:12 am, Matt Jones <[email protected]> wrote:
> On Sep 17, 2:10 pm, Eric <[email protected]> wrote:
>
>
>
> > Thanks for the reply, and I did leave a part out of my schema. There
> > will be other models inheriting from Asset, such that ultimately there
> > will be (at least):
>
> > class Asset < ActiveRecord::Base
> > end
>
> > class Sound < Asset
> >   belongs_to :user, :polymorphic => true
> > end
>
> > class Video < Asset
> >   belongs_to :user, :polymorphic => true
> > end
>
> > class Image < Asset
> >   belongs_to :user, :polymorphic => true
> > end
>
> > class User < ActiveRecord::Base
> >   has_many [:sounds,:videos,:images]
> > end
>
> > The idea will be to have @user.videos, @user.images, etc., so the STI
> > will handle the media types, and the polymorphism is used for User's
> > relationship to each. However, currently user_id is not being saved
> > through the association, and even if I manually set user_id in Asset
> > (and user_type, just for fun), Video.first.user comes up nil. So, STI
> > is working as expected, but the association is broken in some way that
> > leads me to wonder whether Asset needs some connection with User
> > (unlikely, given the docs and commentary I've been able to find), or
> > whether the STI classes need some more connections to the base class
> > in order for User to pass through the STI.
>
> Not sure what the issue you're experiencing is - I'll have to mock
> some things up and try it. But I'll reiterate that the :polymorphic is
> not needed on those belongs_to declarations. The record that they
> point to (a User) is always in the same table. You wouldn't even need
> it going the other way (some kind of User belongs_to :media, for
> instance).
>
> Oh, and that has_many isn't (AFAIK) valid syntax...
>
> --Matt Jones
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to