Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Ignore preload from scope when using through associations #2834

Closed
pixeltrix opened this Issue · 6 comments

3 participants

@pixeltrix
Owner

Given the following models:

class Manufacturer < ActiveRecord::Base; end
class ProductType < ActiveRecord::Base; end

class Product < ActiveRecord::Base
  belongs_to :manufacturer
  belongs_to :product_type
  has_many :variants
  default_scope preload(:manufacturer, :product_type)
end

class Variant < ActiveRecord::Base
  belongs_to :product
end

class BasketItem < ActiveRecord::Base
  belongs_to :variant
  has_one :product, :through => :variant
  has_one :manufacturer, :through => :product
  has_one :product_type, :through => :product
end

then trying to load either item.product_type or item.manufacturer will raise an ActiveRecord::ConfigurationError due to the preload - the easiest fix is likely to be removing the preload from the join model scope.

@pixeltrix pixeltrix was assigned
@jonleighton
Collaborator

What is the error exactly?

@pixeltrix
Owner

This was the full error message:

ActiveRecord::ConfigurationError: Association named 'manufacturer' was not found; perhaps you misspelled it?

The default scope from the join model is being carried over to the destination model and it's trying to preload an association that doesn't exist.

@sankaranarayanan

which version of rails version you are using, I can't able to replicate this error in rails 3.1.0 version

@pixeltrix
Owner

Rails 3.1, here's some step to repro the issue:

  1. Generate a new app and create some models

    rails new testapp --skip-bundle
    cd testapp
    rails g model Manufacturer name:string
    rails g model ProductType name:string
    rails g model Product manufacturer:references product_type:references name:string
    rails g model Variant product:references description:string price:decimal
    rails g model BasketItem variant:references quantity:integer
    rake db:migrate
  2. Setup the models as described above

  3. Generate some test data

    rails c
    >> Manufacturer.create! :name => 'Apple'
    >> ProductType.create! :name => 'Mobile Phone'
    >> Product.create! :name => 'iPhone 4', :product_type => ProductType.first, :manufacturer => Manufacturer.first
    >> Product.first.variants.create! :description => '16GB White', :price => '199.99'
    >> BasketItem.create! :variant => Variant.first, :quantity => 1
  4. Try and load the nested through association

    >> BasketItem.first.manufacturer
    => ActiveRecord::ConfigurationError: Association named 'manufacturer' was not found; perhaps you misspelled it?
  5. Remove the preload from the default scope

    >> BasketItem.first.manufacturer
    => #<Manufacturer id: 1, name: "Apple", created_at: "2011-09-05 15:54:29", updated_at: "2011-09-05 15:54:29">
@jonleighton jonleighton closed this issue from a commit
@jonleighton jonleighton Nested through associations: preloads from the default scope of a thr…
…ough model should not be included in the association scope. (We're already excluding includes.) Fixes #2834.
d4b9829
@sleeptillseven sleeptillseven referenced this issue from a commit
@jonleighton jonleighton Nested through associations: preloads from the default scope of a thr…
…ough model should not be included in the association scope. (We're already excluding includes.) Fixes #2834.
9bde73f
@sankaranarayanan

class Product < ActiveRecord::Base
belongs_to :manufacturer
belongs_to :product_type
has_many :variants
default_scope eager_load(:manufacturer, :product_type)
end
I have replaced preload with eager_load in previous example will raise on same error
ActiveRecord::ConfigurationError: Association named 'manufacturer' was not found; perhaps you misspelled it?
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_dependency.rb: build'

@jonleighton
Collaborator

Fixed 1437690

@ttosch ttosch referenced this issue from a commit
@jonleighton jonleighton Nested through associations: preloads from the default scope of a thr…
…ough model should not be included in the association scope. (We're already excluding includes.) Fixes #2834.
f292d95
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.