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.

Module table_name_prefix not respected for using set_table_name

#5307

Using rails 3.0.0.rc

A module's table_name_prefix method is not respected in two cases:

1) vanilla - the prefix is ignored and uses pluralized module name
2) when using set_table_name - prefix is ignored entirely

rails g model testing/hello

module Testing
  def self.table_name_prefix
    'testing_'
  end
end

class Testing::Hello < ActiveRecord::Base
end

p Testing::Hello.table_name # testings_hellos

class Testing::Hello < ActiveRecord::Base
  set_table_name :hello
end

p Testing::Hello.table_name # hello

1 is obviously a problem, and I think in the case of 2 the prefix ought to still be respected even though the table name was specified.

Reported by jpfuentes2 (at gmail) · August 5th, 2010 @ 03:32 PM

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

Activity

  1. jpfuentes2 (at gmail)
    jpfuentes2 (at gmail)

    As far as 1 is concerned, it seems like it has something to do with autoloading models. When I do the following, the prefix is respected:

    module Testing
      def self.table_name_prefix
        'testing_'
      end
    end
    
    class Testing::Hello < ActiveRecord::Base
    end
    
    p Testing::Hello.table_name # testings_hellos
    
    Testing::Hello.reset_table_name
    
    p Testing::Hello.table_name # testing_hellos
    

    August 7th, 2010 @ 10:07 PM

  2. jpfuentes2 (at gmail)
    jpfuentes2 (at gmail)
    • Tag changed from 3.0.0.rc, active_record, set_table_name, table_name_prefix to 3.0.0.rc, active_record, patch, set_table_name, table_name_prefix

    Including a patch to fix #2 rake rails:freeze:edge on windows. set_table_name now accepts :prefix option like so

    module Testing
      def self.table_name_prefix
        'prefix_'
      end
    end
    
    class Testing::Hello < ActiveRecord::Base
      set_table_name :hiya, :prefix => true
    end
    
    p Testing::Hello.table_name # prefix_hiya
    

    August 7th, 2010 @ 11:06 PM

  3. Andrea Campi
    Andrea Campi

    +1

    The attached patch doesn't apply (a whitespace at the end of the first comment line was removed), can you reroll it?
    Apart from that, I verified the issue and your fix.

    As far as #1 Migrations will not run on a fresh database is concerned, I could not duplicate it, I could not reproduce it on either 3.0.0 or edge.

    October 9th, 2010 @ 05:52 PM

  4. jpfuentes2 (at gmail)
    jpfuentes2 (at gmail)

    (on Rails 3.0.1)

    To be clear on #1 Migrations will not run on a fresh database the module's table_name_prefix is ignored because rails simply uses the module's name instead of the provided prefix:

    module Testing
      def self.table_name_prefix
        'custom_'
      end
    end
    
    class Testing::Hello < ActiveRecord::Base
    end
    
    p Testing::Hello.table_name # testing_hellos
    

    Also, the patch applies just fine to 3.0.1 - should I try applying to edge?

    git apply --stat patch
     activerecord/lib/active_record/base.rb  |   12 ++++++++++--
     activerecord/test/cases/modules_test.rb |   15 +++++++++++++++
     2 files changed, 25 insertions(+), 2 deletions(-)
    

    October 21st, 2010 @ 06:54 PM

  5. Andrea Campi
    Andrea Campi
    • Tag changed from 3.0.0.rc, active_record, patch, set_table_name, table_name_prefix to active_record, patch, rails3, set_table_name, table_name_prefix

    Not sure what's the difference between me and you, but I still cannot reproduce on either 3.0.0 or 3.0.1 (or edge).
    Copy and pasting your exact code gives me:

    ruby-1.9.2-p0 > module Testing
    ruby-1.9.2-p0 ?>    def self.table_name_prefix
    ruby-1.9.2-p0 ?>        'custom_'
    ruby-1.9.2-p0 ?>      end
    ruby-1.9.2-p0 ?>  end
     => nil 
    ruby-1.9.2-p0 > 
    ruby-1.9.2-p0 >   class Testing::Hello < ActiveRecord::Base
    ruby-1.9.2-p0 ?>  end
     => nil 
    ruby-1.9.2-p0 > 
    ruby-1.9.2-p0 >   p Testing::Hello.table_name # testing_hellos
    "custom_hellos"
     => "custom_hellos"
    

    Regarding the patch, yes please, it should go to edge (master) first.

    October 22nd, 2010 @ 02:47 PM

  6. jpfuentes2 (at gmail)
    jpfuentes2 (at gmail)

    Here's the new patch applied to edge (master). You are right about the 1st case, it was not working in rails 3 pre, but is working now with 3.0.0 and 3.0.1.

    October 22nd, 2010 @ 06:50 PM

  7. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    November 8th, 2010 @ 08:50 AM

  8. Paul Eipper
    Paul Eipper

    I believe the :prefix option should be true by default for set_table_name

    February 4th, 2011 @ 08:14 PM

  9. rails
    rails
    • State changed from new to open

    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.

    May 5th, 2011 @ 01:00 AM

  10. rails
    rails
    • State changed from open to stale

    May 5th, 2011 @ 01:00 AM