我有几节课:
class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :member, polymorphic: true
end
class User < ActiveRecord::Base
has_many :memberships, as: :member, dependent: :destroy
has_many :groups, through: :memberships
end
class Package < ActiveRecord::Base
has_many :memberships, as: :member, dependent: :destroy
has_many :groups, through: :memberships
end
和
class Group < ActiveRecord::Base
has_many :memberships, dependent: :destroy, inverse_of: :member
has_many :packages, through: :memberships, source: :member, source_type: 'Package'
has_many :users, through: :memberships, source: :member, source_type: 'User'
end
用户和包可以拥有群组成员资格。在我尝试获取属于组的用户之前,所有这些似乎都可以。在控制台中,使用Group g的实例:
[5] pry(main)> g.users
User Load (3.3ms) SELECT "USERS".* FROM "USERS" INNER JOIN "MEMBERSHIPS" ON
"USERS"."ID" = "MEMBERSHIPS"."MEMBER_ID" WHERE "MEMBERSHIPS"."GROUP_ID" = :a1 AND
"USERS"."MEMBER_TYPE" = 'User' [["group_id", 10000]]
OCIError: ORA-00904: "USERS"."MEMBER_TYPE": invalid identifier:
SELECT "USERS".*
FROM "USERS"
INNER JOIN "MEMBERSHIPS" ON "USERS"."ID" = "MEMBERSHIPS"."MEMBER_ID"
WHERE "MEMBERSHIPS"."GROUP_ID" = :a1
AND "USERS"."MEMBER_TYPE" = 'User'
很容易看到错误 - Users表没有Member_Type列,它驻留在Membership表中。
为什么列没有正确别名?其他人有这个问题吗?
更新
这是Squeel的问题 - 将其从我的Gemfile中删除,并且关联行为正常。