Stephan Schubert
2006-Jul-26 21:02 UTC
[Rails] Polymorphic Association with Single Table Inheritance?
Hello, is it possible to setup a model/table schema like this: Groupable --> Membership <-- Group ^ ^ | | User UserGroup I tried the following but failed: Groupable (table with ''type'' column) has_many :memberships, :as => :groupable has_many :groups, :through => :memberships Membership belongs_to :groupable, :polymorphic => true belongs_to :group, :polymorphic => true Group (table with ''type'' column) has_many :memberships, :as => :group has_many :groupables, :through => :memberships And the inherited tables: User < Groupable has_many :user_groups, :through => :memberships UserGroup < Group has_many :users, :through => :memberships Setting up an user, a membership and an user_group works, but it throws an exception on accessing user_group.users:>> ruby script/console >> u = User.create >> m = Membership.create >> ug = UserGroup.create >> u.memberships << m >> ug.memberships << m >> ug.usersActiveRecord::HasManyThroughAssociationPolymorphicError: ActiveRecord::HasManyThroughAssociationPolymorphicError Thanks for any help! Greets, Stephan -- Posted via http://www.ruby-forum.com/.
Josh Susser
2006-Jul-27 02:49 UTC
[Rails] Re: Polymorphic Association with Single Table Inheritance?
Stephan Schubert wrote:> Hello, > > is it possible to setup a model/table schema like this: > > Groupable --> Membership <-- Group > ^ ^ > | | > User UserGroup > > > I tried the following but failed: > > Groupable (table with ''type'' column) > has_many :memberships, :as => :groupable > has_many :groups, :through => :memberships > > Membership > belongs_to :groupable, :polymorphic => true > belongs_to :group, :polymorphic => true > > Group (table with ''type'' column) > has_many :memberships, :as => :group > has_many :groupables, :through => :memberships > > > And the inherited tables: > > User < Groupable > has_many :user_groups, :through => :memberships > > UserGroup < Group > has_many :users, :through => :memberships > > > Setting up an user, a membership and an user_group > works, but it throws an exception on accessing > user_group.users: > >>> ruby script/console >>> u = User.create >>> m = Membership.create >>> ug = UserGroup.create >>> u.memberships << m >>> ug.memberships << m >>> ug.users > ActiveRecord::HasManyThroughAssociationPolymorphicError: > ActiveRecord::HasManyThroughAssociationPolymorphicErrorhas_many :through works just fine with STI, however it has some issues with polymorphism. I have an explanation and a partial workaround here: http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through But it doesn''t look like you are doing STI at all, just garden-variety polymorphism. The terminology can be confusing, as generic OOP inheritance allows polymorphism through specialization, meaning a subclass can be used in place of a superclass. But in Rails STI just means the models get their data from the same table, which allows the type information to be encapsulated in the model itself (in the ''type'' field in the table), rather than being placed alongside the groupable_id foreign key in the groupable_type field. You only need to have the type in one place, so only use STI or :as => :typeable. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Stephan Schubert
2006-Jul-27 08:14 UTC
[Rails] Re: Polymorphic Association with Single Table Inheritance?
Josh Susser wrote:> But it doesn''t look like you are doing STI at all, just garden-variety > polymorphism.Ah, got the point now. A table containing all attributes of all possible ''subclasses''.. Thank you, read all your blog entries about rails - pretty good stuff :) Greets, Stephan -- Posted via http://www.ruby-forum.com/.
Leevi Graham
2006-Aug-18 01:34 UTC
[Rails] Polymorphic Association with Single Table Inheritance?
Hi Steven, I was reading your post to the ruby on rails mailing list regarding single table inheritance, polymorphism and groups. (http:// lists.rubyonrails.org/pipermail/rails/2006-July/056582.html) Did you ever get this sorted out? Cheers Leevi Leevi Graham Front End Developer - User Interface Designer www.leevigraham.com | Skype Me: leevi_graham MSN Messenger: info@leevigraham.com Download My VCard -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060818/64502da5/attachment.html
Reasonably Related Threads
- It seems to have bug for @group to set in valid or invalid conf
- validation of acts_as_list in the model of the base class?
- Relationship Proxies?
- OT: finding a designer (bit of a rant)
- 3.0.0-2 on RH9 as domain member of win2k domain - not able to write to shares...