This project is archived and is in readonly mode.
belongs_to with :primary_key fails to preload
-
David Trasbo
- Assigned user set to Ryan Bigg
Rails is not supposed to support foreign key columns of other types than integer. This ticket can be marked as invalid.
-
Ryan Bigg
- State changed from new to wontfix
I agree, this seems like an edge-case better suited to a plugin.
-
Greg Hazel
What? This would also be broken if the key were an integer.
-
Ryan Bigg
- State changed from wontfix to open
Leaving it open for discussion. OP has indicated he does not want to submit a patch.
-
Greg Hazel
- Title changed from belongs_to with non-integer :foreign_key fails to preload to belongs_to with :primary_key fails to preload
-
Greg Hazel
Looks like this was fixed recently:
http://github.com/rails/rails/commit/63026541b209cc11ffd74cf3ca04b8...
-
Greg Hazel
And, guys, if "Rails is not supposed to support foreign key columns of other types than integer", why are they used in the ActiveRecord unit tests themselves?
class Reply < Topic ... belongs_to :topic_with_primary_key, :class_name => "Topic", :primary_key => "title", :foreign_key => "parent_title", :counter_cache => "replies_count" ... end ... create_table :topics, :force => true do |t| t.string :title ... t.string :parent_title ... end -
Jeff Kreeftmeijer
I did a little test on 2.3.5 with the
FooandBarmodels you described (I only changed thesome_idandguidfields to be integers). I'm not getting anyNilClasserrors, it seems to work perfectly:>> Foo.find(2) => #<Foo id: 2, some_id: 123, created_at: "2010-04-16 10:18:32", updated_at: "2010-04-16 10:18:32"> >> Foo.find(2).bar => #<Bar id: 1, guid: 123, created_at: "2010-04-16 10:19:58", updated_at: "2010-04-16 10:19:58"> >> Foo.find(2, :include => :bar) => #<Foo id: 2, some_id: 123, created_at: "2010-04-16 10:18:32", updated_at: "2010-04-16 10:18:32"> -
Greg Hazel
Jeff: Is id the primary_key for Bar? Can you show the sql queries it is submitting? According to line 324 and 335 of associtation_preload.rb in Rails 2.3.5 it should be submitting:
primary_key = klass.primary_key
"#{table_name}.#{connection.quote_column_name(primary_key)} #{in_or_equals_for_ids(ids)}"
which in your case would be:
Bar.id = 123
So, I'm not sure why you can't reproduce it exactly, but the bug certainly existed for integer columns, and was fixed by http://github.com/rails/rails/commit/63026541b209cc11ffd74cf3ca04b8...
-
Jeff Kreeftmeijer
Foo:class Foo < ActiveRecord::Base belongs_to :bar, :foreign_key => 'some_id', :primary_key => 'guid' endFoosController:class FoosController < ApplicationController def index @foo = Foo.find(1, :include => :bar) end endfoos/index.html.erb:<%= @foo.bar.to_yaml %>queries:
Processing FoosController#index (for 127.0.0.1 at 2010-04-16 12:50:15) [GET] Foo Load (0.2ms) SELECT * FROM "foos" WHERE ("foos"."id" = 1) Bar Load (0.2ms) SELECT * FROM "bars" WHERE ("bars"."id" = 123) Rendering foos/index Bar Load (0.3ms) SELECT * FROM "bars" WHERE ("bars"."guid" = 123) LIMIT 1 Completed in 16ms (View: 4, DB: 1) | 200 OK [http://localhost/foos]So I think you're right. It's not properly preloading in 2.3.5 when using integers if you ask me...
-
Jeff Kreeftmeijer
Damn, missed the link. Like Greg said: fixed by http://github.com/rails/rails/commit/63026541b209cc11ffd74cf3ca04b8....
-
Ryan Bigg
- State changed from open to resolved
-
Jamison Wilde
- Importance changed from to Low
There is a additional bug exposed with this fix, in that if you use intern'd column names as your primary key, the comparison then fails.
:primary_key => :my_column fails while :primary_key => 'my_column' succeeds.
The patch is to use to_s:
options[:primary_key].to_sObviously the workaround is to not use intern'd column names.
Putting this here since this is the first place people will find in a search engine presumably.
-
Jamison Wilde
Also, some more background:
This fails silently in Ruby 1.8.In Ruby 1.9 it fails loudly with:
NoMethodError: undefined method
type' for nil:NilClass<br/>from /home/slain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-2.3.8/lib/active_support/whiny_nil.rb:52:in `method_missing'Since the columns.detect fails and Ruby 1.9 no longer supports type as a method on NilClass.