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.

Advanced / foxy fixture features doesn't work well with models in modules

#2965

If models reside within modules then fixture loading doesn't work when you use advanced / foxy fixture features like label references.
The attached patch adds some tests that fail already when loading.
test_relations(FoxyFixturesInModulesTest):<br/> ActiveRecord::StatementInvalid: SQLite3::SQLException: table tracks has no column named album: INSERT INTO "tracks" ("title", "album") VALUES ('Foxy Lady', 'album1')<br/>

One could argue the set_table_name and/or set_fixture_class should be used but that is certainly not convention over configuration and you can't do set_fixture_class when doing a rake db:fixtures:load.

According to #1976 Scaffold generator not working well with namespaced resources so should fixtures for a module possibly be in a folder with the fixture name by default but that makes the current situation even worse.

Reported by kmpm · July 28th, 2009 @ 03:37 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: none

Activity

  1. kmpm
  2. Kane
    Kane

    Hey,

    I took your tests modifed them to reflect #1976 Scaffold generator not working well with namespaced resources so fixtures are in subfolders representing the namespace for organisational purpose. generators should now generate the fixtures and models (no namespace in tablename) correctly.

    also modified fixtures:load but no clue how to test it.

    August 16th, 2009 @ 03:13 AM

  3. José Valim
    José Valim

    @Kane, awesome! I have some small feedback about the patch:

    1) This code repeats in three parts in the patch:

      namespace = Fixtures.extract_namespace(fixture_file)
      class_name = Fixtures.build_class_name(table_name, namespace)
      Fixtures.try_to_load_dependency(class_name.underscore)
      class_name if Fixtures.class_exists? class_name
    

    Could you put it in its own method?

    2) There is a "debug" line in the patch:

    if table_names.include?'albums'
    puts table_names.inspect
    puts class_names.inspect
    end
    

    3) One question, why do you have split, prune and join the namespace?

    namespace = namespace.split('/')[0..-2].join('/')
    

    4) And there is no need to add the generators changes on this patch. We will do that in #1976 Scaffold generator not working well with namespaced resources. :)

    Good work, thanks!

    August 16th, 2009 @ 08:37 AM

  4. Kane
    Kane

    thanks for the feedback

    i removed all generator specific changes and the debug lines.
    also made refactoring to reduce the repeating code. and to 3) i assume you mean line 510, its to remove the classname.

    August 16th, 2009 @ 07:48 PM

  5. Kane
    Kane
    • Assigned user set to Jeremy Kemper

    August 28th, 2009 @ 12:39 AM

  6. gjeudy
    gjeudy

    Is there a workaround for this bug? using set_fixture_class doesn't help in my case. I'm trying the same thing a bidirectional one-to-many association with foxy fixtures.

    December 3rd, 2009 @ 10:19 PM

  7. Andrew White
    Andrew White

    Attached patch adds support for namespaced model fixtures. Fixture name uses '/' to specify the namespace, so to use in a test you'd do something like this:

    class Admin::AccountTest < ActiveSupport::TestCase
      fixtures :"admin/accounts"
    
      def test_something
        assert admin_accounts(:fixture_name)
      end
    end
    

    Code isn't as clean as I'd like but I'm guessing you don't want a major refactoring of fixtures at this point. It should be possible to backport this to 2-3-stable if desired as soon as the backport of #2283 Unnecessary exception raised in AS::Dependencies.load_missing_constant is applied.

    I was in two minds over stripping the namespace off the fixture accessor - what do you think?

    April 14th, 2010 @ 06:30 PM

  8. José Valim
    José Valim
    • Assigned user changed from Jeremy Kemper to José Valim
    • Milestone cleared.

    April 14th, 2010 @ 08:06 PM

  9. Andrew White
    Andrew White

    It would help if I included the fixtures and models. Got burned (again!) by the fact that git commit -a doesn't include new files.

    April 15th, 2010 @ 06:12 AM

  10. José Valim
    José Valim

    Hey Andrey, sorry for the delay but the patch does not apply anymore. Can you please rebase it?
    Also, what is the deal with #2283 Unnecessary exception raised in AS::Dependencies.load_missing_constant? It's marked as resolved, but the problem still persists for you?

    Finally, I'm -1 in back porting these changes to 2.3 since I don't want to make significative changes and break plugins in a maintenance branch.

    April 25th, 2010 @ 12:23 PM

  11. Andrew White
    Andrew White

    Rebased patch attached.

    The issue with #2283 Unnecessary exception raised in AS::Dependencies.load_missing_constant is that I backported the patch but it's never been applied to 2-3-stable. Since I was asked to backport #4032 Module specific table_name_prefix I thought there was an attempt to get nested models working the same in 2.3 and 3.0, which is why I suggested backporting this patch.

    April 25th, 2010 @ 02:46 PM

  12. Repository
  13. José Valim
    José Valim

    Awesome, thanks for the patch! Imho, there is no need to backport for 2-3-stable. If anyone else is interested, they can apply it.

    April 25th, 2010 @ 03:06 PM

  14. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM