BUG? has_many :through makes funny queries

So I have this structure.

class Gallery < ActiveRecord::Base
  belongs_to :owner, :polymorphic => true
  has_many :folders, :order => 'slug', :dependent => :destroy

class Folder < ActiveRecord::Base
  has_many :photos, :dependent => :destroy, :conditions => "parent_id IS
  belongs_to :gallery

class User < ActiveRecord::Base
  has_one :gallery, :as => :owner, :dependent => :destroy
  has_many :folders, :through => :gallery

Seems fine, right? So @user.folders should do join between users,
galleries and folders.

users.id (1 in the example) == gallery.owner_id and gallery.id ==

Right? Well it does not.

Mysql::Error: #42S22Unknown column 'galleries.gallery_id' in 'on
clause': SELECT folders.* FROM folders INNER JOIN galleries ON
folders.id = galleries.gallery_id WHERE ((galleries.owner_id = 1) AND
(galleries.owner_type = 'User'))

This query doesn't make a lot of sense for me... Anyone wanna explain?