This project is archived and is in readonly mode.

#3027 ✓stale

Association Table Names Incorrectly Generated when set_table_name used

Reported by feldercarb | August 9th, 2009 @ 10:17 PM

Rails 2.3.2, Ruby 1.8.7.

I have 2 models for a legacy database which use set_table_name to id their tables. This is due to a renaming of the model as part of a data migration.

Each model has a HABTM relationship with the other table through an unmodelled association ta
The table name for the unmodelled association table is incorrectly generated during find/first/all lookup operations.


Table 1: schedules
Table 2: timeslots
Association Table: schedules_timeslots

Model 1: V2Schedule
Model 2: V2Timeslot

Computed Association Table Name => v2_schedules_v2_timeslots


If set_table_name is used for a model, the value from set_table_name should be used for the join table name.

Comments and changes to this ticket

  • CancelProfileIsBroken
  • Elad Meidar

    Elad Meidar September 25th, 2009 @ 11:44 PM

    +1 verified

    if join_table is missing from the macro options, the join table name will be calculated on associations.rb#1596, by using undecorated_table_name (
    This has obviously no reference to the actual table name, if set by set_table_name.

    I tried to patch that line to:

    reflection.options[:join_table] ||= join_table_name(self.table_name, reflection.class_name.constantize.table_name)

    but reflection.class_name is out of scope in this context, if anyone has any idea how to access/load the association AR::Base class, that would be the key to access the current table name.

  • Robert Rouse
  • Robert Rouse

    Robert Rouse September 26th, 2009 @ 09:21 PM

    Here is what I've found so far.

    Changing the line at 1596 in associations.rb (as Elad said) to the following does make it appear to work:

    reflection.options[:join_table] ||= join_table_name(undecorated_table_name(self.table_name), undecorated_table_name(reflection.class_name.constantize.table_name))

    I've tried it in a sample Rails app and it returns the right table name no matter which side of the association I use set_table_name on.

    However, this causes the tests in AR to fail with uninitialized constant Project in the belongs_to_association_test.rb file.

    I'm guessing this is what Elad meant before and it working for me is just a funny coincidence.

    I'm still not sure what's up there.

  • Anuj Dutta

    Anuj Dutta September 26th, 2009 @ 09:54 PM

    Project is the name of one of the models insides the activerecord/test/models directory where has_and_belongs_to_many has been defined and which is used for running the tests. The error basically means that your change has broken something.

  • Robert Rouse

    Robert Rouse September 26th, 2009 @ 10:06 PM


    I know what the model is. I'm not sure why it's broken something.

  • Rizwan Reza
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:44 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “”

    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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:44 PM

    • 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=""></a>

People watching this ticket

Referenced by