This project is archived and is in readonly mode.
Bad self referential HABTM associations should fail fast.
Reported by Tom Lea | October 22nd, 2008 @ 11:36 PM | in 2.x
Self referential HABTM associations should not be allowed to be created unless the association_foreign_key has been manually assigned.
If this is not prevented, we end up with an join table which has only one column, which is referenced twice... causing odd and confusing results (which caused me a headache!).
This patch causes trying to do this to fail fast, saving on debug time.
Rough Example:
class Person < AR::Base
habtm :friends, :join_table => :person_friend_pairings
end
alice = Person.create! :name => "Alice" # id => 1
bob = Person.create! :name => "Bob" # id => 2
alice.friends << bob
alice.reload
alice.friends.to_a # SELECT * FROM person_friend_pairings where person_id = 2
Tagged as an enhancement, but could be a bug... feel free to change it.
Comments and changes to this ticket
-
Steven Soroka October 27th, 2008 @ 05:16 PM
isn't this because the habtm isn't set up properly? don't you need to identify the names of the foreign_key and association_foreign_key ?
Sounds invalid to me.
-
Tom Lea October 27th, 2008 @ 05:48 PM
Indeed it is... but the fact that it pretends to work is the issue.
The whole thing seems to work just fine, we were several unit tests in before it started behaving oddly. Anyone who tests less thoroughly than we do here could well be cough out.
The patch makes it fail fast, not start magically guessing things.
-
Tom Lea October 28th, 2008 @ 12:41 AM
New patch, error message was not as expected. Copied through relationship exception format by accident... oops.
Also rebased against HEAD while I was at it.
-
Repository March 6th, 2009 @ 06:28 PM
- State changed from new to resolved
(from [c896d56c6e5520346c260477f7d7f8bf951e72cc]) Ensure self referential HABTM associations raise an exception if association_foreign_key is missing. [#1252 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
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>
People watching this ticket
Attachments
Referenced by
- 1252 Bad self referential HABTM associations should fail fast. (from [c896d56c6e5520346c260477f7d7f8bf951e72cc]) Ensure ...