rubyonrails - beware of similarly named relationships
29 Apr 2006I recently developed a rubyonrails application in which a user had a province (as part of their address) and also a collection of selected provinces.
I could very easily find out which provinces a user had selected and also which users had selected a specific province. However I didn’t realise that if I accessed a user via the province then the province_id
which previously pointed the user’s address province was replaced by the id of the selected province.
I assume the province_id
is added by the habtm join so that we can understand which selected_province
object this user object comes from however in my case it meant that I lost the original province_id
and so if I saved that user then I would end up changing the address province.
This is probably not a bug as such but something that should be watched out for as it took me a while to discover this problem with my application.