Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Namespaced models and STI

#114

When using namespaced models and STI, ActiveRecord stores the demodulized name of the class. This work if all the class in the STI hierarchy are in the same namespace but will break otherwise.

Example:

class CollectionItem < ActiveRecord::Base; end
class ComicCollection::Item < CollectionItem; end

item = ComicCollection::Item.new
item.type # => 'Item'

item2 = CollectionItem.find(item.id) # raise an error because can't find class Item

This patch add an option that when enable (it is by default) will store the full class name.

Reported by Rodrigo Kochenburger · May 5th, 2008 @ 03:41 PM

State: resolved
Milestone: 2.1.1
Assigned to: Rick Rick
Importance: none

Activity

  1. Pratik
    Pratik
    • State changed from new to incomplete

    Why the need to add new tables for tests ? It should be possible to use existing tables.

    Rest looks good.

    Thanks!

    May 11th, 2008 @ 10:45 PM

  2. Rick
    Rick
    • Milestone set to 2.1.1
    • Assigned user set to Rick

    I really want this in 2.1. Though, the default should be off to keep the existing behavior, and on for Rails 3.0 and forward.

    I also agree that the new tables for tests aren't necessary.

    May 13th, 2008 @ 09:51 PM

  3. Rick
    Rick

    Also, I'm going to remove the choice since it shouldn't break for anyone. Anyone using STI without namespaced models will be unaffected, and this bug is basically preventing anyone from using STI with namespaced models.

    May 13th, 2008 @ 09:55 PM

  4. Rick
    Rick
    • State changed from incomplete to open

    May 13th, 2008 @ 11:23 PM

  5. Repository
    Repository

    (from [bca8751e40a5594c4de2ca58e089b8d98e44632b]) Add ActiveRecord option to store the full class name on STI's type column, allowing one to have STI subclasses in different namespaces [#114]

    Signed-off-by: rick

    http://github.com/rails/rails/co...

    May 13th, 2008 @ 11:23 PM

  6. Jonathan Viney
    Jonathan Viney

    HasManyThroughAssociation#build_sti_condition needs to be addressed as well.

    May 14th, 2008 @ 03:13 AM

  7. Rodrigo Kochenburger
    Rodrigo Kochenburger

    That's great. Thanks Rick.

    About having extra table, sorry about that. I did that to avoid breaking other things, my bad.

    May 14th, 2008 @ 01:18 AM

  8. Rick
    Rick

    No worries.

    Just FYI there are three issues remaining:

    • HasManyThroughAssociation#build_sti_condition, as Jonathan mentioned above.
    • ActiveRecord::Base#type_name_with_module adds the parent name, resulting in an annoying ruby warning.
    • You can't create migration-specific STI models because the migration name will be saved to the #type value too. (see the rails core ML post.

    May 14th, 2008 @ 03:14 AM

  9. Repository
    Repository
    • State changed from open to resolved

    (from [72483c0d4c1e4ea794919974100acc2f255f6fd2]) Add ActiveRecord::Base.sti_name that checks ActiveRecord::Base#store_full_sti_class? and returns either the full or demodulized name. [rick] [#114 state:resolved]

    http://github.com/rails/rails/co...

    June 1st, 2008 @ 01:15 AM