class User < ActiveRecord::Base
has_many :memberships
has_many :accounts, :through => :memberships, :source => 'membershipable', :source_type => 'Account'
has_many :projects, :through => :memberships, :source => 'membershipable', :source_type => 'Project'
end
class Account < ActiveRecord::Base
has_many :memberships, :as => :membershipable
has_many :users, :through => :memberships
end
class Project < ActiveRecord::Base
has_many :memberships, :as => :membershipable
has_many :users, :through => :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :membershipable, :polymorphic => true
end
代码很简单,需求就是把 account 和 project 所属的用户都记录在 membership 中。
User.first.projects << Project.first 不工作 NameError: uninitialized constant Membership::Membershipable
不知道大家在 rails 3.1.1 下用 has_many :through 和 Polymorphic Associations 是否出现过这样的问题?
我记得在以前的 rails 版本中是支持的。今天发现 3.1.1 不支持了,feature or bug?