I’ve run into an interesting problem that I have been unable to solve.
My code is in the following pastie: Parked at Loopia
User goes to /tag/tagname/, the action finds the tag with that slug and
uses that to find posts with that tag. Unfortunately, for whatever
reason, eager loading those Post’s Tags just doesn’t work. I’ve tried
the following:
This example is the one I pretty much saw on every site dealing with
eager loading with has_many :through. It doesn’t work, though. I get
the error: “Association named ‘tags’ was not found; perhaps you
misspelled it?” No I didn’t misspell it, has_many :tags is right in my
Post model.
@tags.posts.find(:all, :include => {:taggings => :tags})
This gives me no error, HOWEVER, based on development logs, queries are
still being run during rendering. So it’s not halting the app but at
the same time it’s not actually doing anything. And yes, :tag is meant
to be singular in this example, although I’m not sure why AR likes this
more than plural (?), I’m using has_many :tags, not has_one :tag :
@tags.posts.find(:all, :include => {:taggings => :tag})
This gives me the error, “Mysql::Error: #42000Not unique table/alias:
‘taggings’:”. I don’t know what that’s supposed to mean.
@tags.posts.find(:all, :include => [:tags])
However, this works fine, no errors, and no queries during rendering:
Post.find(:all, :include => [:tags])
But of course that gives me all Posts instead of Posts with a specific
Tag. I am baffled as to why the last example succeeds where the other
ones fail.
Really clueless here. Please help