Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10517 (closed defect: fixed)

Opened 7 months ago

Last modified 7 months ago

[PATCH] Can not :include a belongs_to association with a foreign key inferred from the association name

Reported by: jonathan_viney Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

The title says it all. My use case is:

class Configuration < ActiveRecord::Base
  belongs_to :default_address, :class_name => "Address"
end

Configuration.find(:first, :include => :default_address)

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'configurations.address_id' in 'on clause'

Obviously it should be looking for default_address_id not address_id.

The patch fixes the issue in associations.rb and adds a test by altering an existing fixture class to use the inferred foreign key.

Attachments

include_belongs_to_with_inferred_foreign_key.patch (1.8 kB) - added by jonathan_viney on 12/16/07 01:03:23.
belongs_to_with_inferred_foreign_key_and_docs.patch (3.1 kB) - added by jonathan_viney on 12/16/07 02:34:17.
Add documentation changes

Change History

12/16/07 01:03:23 changed by jonathan_viney

  • attachment include_belongs_to_with_inferred_foreign_key.patch added.

12/16/07 01:10:54 changed by lifofifo

+1

12/16/07 02:00:02 changed by fxn

The API docs say the foreign key is guessed from the _class name_, so it works as documented:

:foreign_key - specify the foreign key used for the association. By default this is guessed to be the name of the associated class in lower-case and _id suffixed. So a Person class that makes a belongs_to association to a Boss class will use boss_id as the default foreign_key.

12/16/07 02:08:43 changed by lifofifo

API docs are wrong. This behavior was changed in [7188]. So yeah, please update the API docs too :-) This is a defect in eager loading code.

12/16/07 02:34:17 changed by jonathan_viney

  • attachment belongs_to_with_inferred_foreign_key_and_docs.patch added.

Add documentation changes

12/16/07 10:09:45 changed by fxn

Good.

12/21/07 01:49:07 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [8456]) Eager belongs_to :include infers the foreign key from the association name rather than the class name. Closes #10517.