This project is archived and is in readonly mode.
Finder :include assoc with same name as key fail on Postgres
Reported by Martin Sadler | May 29th, 2008 @ 05:31 PM
On Postgres when calling find on a model with an :include that contains an association with the same name as it's key an exception is raised:
>> Project.find(:all, :include => 'created_by')
=> Message: <"PGError: ERROR: invalid input syntax for integer: \"#<Person:0x19e0038>\"\n: SELECT * FROM \"people\" WHERE (\"people\".id IN (E'#<Person:0x19e0038>')) ">
The basic setup is as follows:
create_table :projects, :force => true do |t|
t.string :name
t.string :type
t.integer :created_by
end
class Project < ActiveRecord::Base
belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by'
...
end
This largely effects people who have used created_by, updated_by in their models. See: http://wiki.rubyonrails.org/rail...
However there is likely to be other edge cases
I have attached the failing test case and working fix as a patch.
Notes:
1) An argument against this patch could be that best practice should be to use a column named created_by_id instead. However I would probably favor a deprecation notice / warning over this to begin with.
2) This currently only affects Postgres and not MySQL. Given both adapters use the same code fix it suggests there maybe a deeper issue present.
Comments and changes to this ticket
-
Frederick Cheung May 29th, 2008 @ 10:59 PM
It seems to me that you are already skating on thin ice by doing this.
-
Martin Sadler May 30th, 2008 @ 08:26 AM
Frederick, thanks for the comment. Would you care to expand on that?
-
Frederick Cheung May 30th, 2008 @ 10:00 AM
Just stuff that would normally work like update_attributes (eg if you were updating a record) and generally other stuff that assumes it can use the accessors to access an attribute (which code usually should because you could have overridden that accessor with good reason)
-
Pratik July 2nd, 2008 @ 02:18 AM
- State changed from new to incomplete
- Tag set to activerecord, associations, bug, edge, patch, postgres
I'm getting 2 tests failures on MySQL after applying this.
I kinda agree with Fred that you're already walking on thin ice, if not skating.
I think it'd be much easier if you just call your association 'creator' or something similar, which is not a column name.
But if the fix is very simple ( as it is in the patch ), probably wouldn't hurt to apply.
-
Martin Sadler July 2nd, 2008 @ 08:46 AM
Hey Pratik, I would close this ticket for now.
I ended up renaming created_by/updated_by to created_by_id/updated_by_id in the database and made a few tweaks to the plugin to handle the change.
Makes more sense and goes with the Rails way of thinking.
-
Pratik July 2nd, 2008 @ 12:51 PM
- State changed from incomplete to wontfix
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>