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.

Test env ignores AR set_table_name

#5100

I have legacy tables that uses studdly caps for table and column names, so the models must explicit set the table and primary key:

  class InventoryBlueprintType < ActiveRecord::Base
    set_table_name "invBlueprintTypes"
    set_primary_key "blueprintTypeID"
  end

While the app works, all of the model tests would fail with NoMethodError: undefined method eq for nil:NilClass. Perplex I rummaged around until I luckily tried loading models in the test console vs the development console.

imperator:theapp zinger$ rails console test
Loading test environment (Rails 3.0.0.beta4)
>> InventoryBlueprintType
=> InventoryBlueprintType(Table doesn't exist)
>> exit
imperator:theapp zinger$ rails console
Loading development environment (Rails 3.0.0.beta4)
>> InventoryBlueprintType
=> InventoryBlueprintType(blueprintTypeID: integer, parentBlueprintTypeID: integer, productTypeID: integer, productionTime: integer, techLevel: integer, researchProductivityTime: integer, researchMaterialTime: integer, researchCopyTime: integer, researchTechTime: integer, productivityModifier: integer, materialModifier: integer, wasteFactor: integer, maxProductionLimit: integer)
>> exit

The above shows the RAILS_ENV for test is not using the model's set_table_name, while the RAILS_ENV for development is. It looks like the NoMethodError: undefined method eq for nil:NilClass error is happening from AR joins to the tables with set_table_name.

Reported by mguymon · July 12th, 2010 @ 10:18 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • Importance changed from to Low

    mguymon, is this still an issue with Rails 3.0?

    September 13th, 2010 @ 02:20 AM

  2. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Tag changed from rails 3.0.0.beta4, testing to rails 300beta4, testing

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:31 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:31 PM

  4. Andrew Burns
    Andrew Burns

    I just ran into this with AR 3.0.5 and think I found the culprit, an edge case (obviously). The set_table_name does not match the table name in the connection.tables array. For me this was caused by passing a string into set_table_name (because it was easier to copy and paste from the legacy system), changing this to a symbol resolved my issue.

    Here are the relevant portions of my rails c test session:

    Loading test environment (Rails 3.0.5)
    irb(main):001:0> a = ChadApplication.find(1)
    NoMethodError: undefined method `eq' for nil:NilClass
            ...snip stack trace...
    
    irb(main):002:0> ActiveRecord::Base.connection.tables
    => ["appconfigodbc", "appconfigodbcprop", "appconfigoraclenet", "appconfigregistry", 
    "application", "applicationcomponent", "applicationconfig", "applicationtype", 
    "registrykey", "sequencereference", "userapplicationconfig", "userconfig", 
    "schema_migrations"]
    
    irb(main):003:0> ChadApplication.table_name
    => "APPLICATION"
    
    irb(main):004:0> ChadApplication.table_name = 'application'
    => "application"
    
    irb(main):005:0> a = ChadApplication.find(1)
    ActiveRecord::RecordNotFound: Couldn't find ChadApplication with ID=1
            ...snip stack trace...
    

    March 9th, 2011 @ 11:34 PM