nullnullnull
nullnullnull

Reputation: 8189

Rails: Association was not found; perhaps you misspelled it?

I have a User model with a has_many :through relationship to the Publication model. The Publication model in turn has a has_many :through relationship to Author:

class User < ActiveRecord::Base
  has_many :library_publications, :dependent => :destroy, :class_name => "Library::Publication"
  has_many :publications, :through => :library_publications
end

class Library::Publication < ActiveRecord::Base
  belongs_to :publication
  belongs_to :user
end

class Publication < PublicationBase
  has_many :library_publications, :dependent => :destroy, :class_name => "Library::Publication"
  has_many :users, :through => :library_publications
  has_many :publication_contributions, :dependent => :destroy, :class_name => "Publication::Contribution"
  has_many :authors, :through => :publication_contributions
end

class Author < AuthorBase
  has_many :publication_contributions, :dependent => :destroy, :class_name => "Publication::Contribution"
  has_many :publications, :through => :publication_contributions
end

class Publication::Contribution < Publication::ContributionBase
  belongs_to :publication, :class_name => "Publication"
  belongs_to :author, :class_name => "Author"
end

As far as I can tell, all the associations are written correctly. However, when I try to eagerload authors from a user:

@user.library_publications.includes(:publication => [:authors])

I get this error:

Association named 'authors' was not found; perhaps you misspelled it?

What might be the cause of this?

Upvotes: 0

Views: 2327

Answers (1)

nullnullnull
nullnullnull

Reputation: 8189

After experimenting a little, I discovered that all of publication's associations were broken. This led to me to looking for larger problems, and eventually I discovered that this issue was caused by one of the join-table being namespaced, Library::Publication. When I de-namespaced it, publication's associations began working again.

I'm not sure why this happened, though. If anyone has an explanation, please share.

Upvotes: 2

Related Questions