Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6519 (closed defect: duplicate)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Make nested models using STI work correctly

Reported by: jonathan_viney Assigned to: bitsweat
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: sti nested module
Cc: obrie

Description

Why is the class name stored in the inheritance column demodulized? This breaks STI in nested model classes because only the last class name is stored.

Patch with tests attached.

Attachments

nested_sti.patch (2.8 kB) - added by jonathan_viney on 10/30/06 12:44:08.

Change History

10/30/06 12:36:52 changed by jonathan_viney

  • version set to edge.

10/30/06 12:44:08 changed by jonathan_viney

  • attachment nested_sti.patch added.

10/30/06 12:54:14 changed by jonathan_viney

A simple example from the patch:

class Post < ActiveRecord::Base
end

class Post::NestedStiPost < Post
end

nested_sti_post = Post::NestedStiPost.create!
Post.find(nested_sti_post.id) # raises an exception because it tries to instantiate NestedStiPost, not Post::NestedStiPost

This patch may be backwards incompatible because of the changes to what is stored in the inheritance column. It breaks a couple of existing tests because of this problem.

12/01/06 00:22:16 changed by obrie

This is a problem I've been running into for a long, long time now. There a few places where this patch doesn't address, primarily eager loading. I'll attach a modified patch with the additional fixes.

+1 for this being a Rails 2.0 patch. It is much needed.

12/01/06 22:11:55 changed by bitsweat

  • keywords set to sti nested module.
  • owner changed from David to bitsweat.
  • milestone changed from 1.2 to 1.x.

Agreed. It'd be nice to provide a sample migration to translate old -> new types.

12/08/06 22:40:54 changed by bitsweat

  • status changed from new to closed.
  • resolution set to duplicate.

02/14/07 20:52:52 changed by dkubb

Is this really a duplicate of #6792 ?

It seems to me that ticket #6792 is advocating using demodulize in inheritance columns, while this ticket advocates the opposite: removing demodulized class names from inheritance columns.

If it were up to me we would NOT use demodulize in inheritance columns, so I agree with this ticket. I've run into my share of problems with the current approach.

Perhaps there would be a way to make this configurable. We could set the following by default:

 ActiveRecord::Base.demodulize_class_names_in_associations = true
}}

But display deprecation warnings telling people to explicitly set it to false and run a migration script.  For Rails 2.0 it could be flipped to false by default.

(follow-up: ↓ 8 ) 02/19/07 18:52:37 changed by dkubb

  • status changed from closed to reopened.
  • resolution deleted.

Reopening to get some comment on whether or not this is really a duplicate of ticket #6792.

The approach this ticket takes appears to be the opposite of that one, so I am not sure why it was considered a dupe. Would someone care to enlighten me on why?

(in reply to: ↑ 7 ) 02/19/07 18:58:35 changed by obrie

Replying to dkubb:

Reopening to get some comment on whether or not this is really a duplicate of ticket #6792. The approach this ticket takes appears to be the opposite of that one, so I am not sure why it was considered a dupe. Would someone care to enlighten me on why?

I believe it was because the author changed his mind in #6792 and decided against using demodulize, making it then a duplicate of this ticket since they are both now proposing removing .demodulize from STI. At least .. I think that's why :)

02/19/07 18:58:50 changed by obrie

  • cc set to obrie.

02/19/07 20:01:28 changed by dkubb

  • status changed from reopened to closed.
  • resolution set to duplicate.

Ahh obrie, I re-read that ticket thread and you're right. Closing this ticket.