This project is archived and is in readonly mode.

#5307 ✓stale
jpfuentes2 (at gmail)

Module table_name_prefix not respected for using set_table_name

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

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.

Comments and changes to this ticket

  • jpfuentes2 (at gmail)

    jpfuentes2 (at gmail) August 7th, 2010 @ 10:07 PM

    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
    
  • jpfuentes2 (at gmail)

    jpfuentes2 (at gmail) August 7th, 2010 @ 11:06 PM

    • 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. 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
    
  • Andrea Campi

    Andrea Campi October 9th, 2010 @ 05:52 PM

    +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 is concerned, I could not duplicate it, I could not reproduce it on either 3.0.0 or edge.

  • jpfuentes2 (at gmail)

    jpfuentes2 (at gmail) October 21st, 2010 @ 06:54 PM

    (on Rails 3.0.1)

    To be clear on #1 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(-)
    
  • Andrea Campi

    Andrea Campi October 22nd, 2010 @ 02:47 PM

    • 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.

  • jpfuentes2 (at gmail)

    jpfuentes2 (at gmail) October 22nd, 2010 @ 06:50 PM

    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.

  • Jeff Kreeftmeijer

    Jeff Kreeftmeijer November 8th, 2010 @ 08:50 AM

    • Tag cleared.
    • Importance changed from “” to “Low”

    Automatic cleanup of spam.

  • Paul Eipper

    Paul Eipper February 4th, 2011 @ 08:14 PM

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

  • rails

    rails May 5th, 2011 @ 01:00 AM

    • 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.

  • rails

    rails May 5th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages