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.

Association Table Names Incorrectly Generated when set_table_name used

#3027

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.

e.g:

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

Model 1: V2Schedule
Model 2: V2Timeslot

Computed Association Table Name => v2_schedules_v2_timeslots

BUG:

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

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

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

Activity

  1. CancelProfileIsBroken
  2. Elad Meidar
    Elad Meidar

    +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 (http://apidock.com/rails/ActiveRecord/Base/undecorated_table_name/c....
    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.

    September 25th, 2009 @ 11:44 PM

  3. Robert Rouse
  4. Robert Rouse
    Robert Rouse

    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.

    September 26th, 2009 @ 09:21 PM

  5. Anuj Dutta
    Anuj Dutta

    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.

    September 26th, 2009 @ 09:54 PM

  6. Robert Rouse
    Robert Rouse

    andhapp:

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

    September 26th, 2009 @ 10:06 PM

  7. Rizwan Reza
  8. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:44 PM

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

    February 2nd, 2011 @ 04:44 PM