3
我的模式是这样的:的has_many:通过与:foreign_key
class Post < ActiveRecord::Base
has_many :aspect_visibilities, :as => :shareable, :primary_key => :guid, :foreign_key => :shareable_guid
has_many :aspects, :through => :aspect_visibilities
end
class AspectVisibility < ActiveRecord::Base
belongs_to :aspect
validates_presence_of :aspect
belongs_to :shareable, :polymorphic => true, :primary_key => :guid, :foreign_key => :shareable_guid
validates_presence_of :shareable
end
class Aspect < ActiveRecord::Base
has_many :aspect_visibilities
has_many :posts, :through => :aspect_visibilities, :source => :shareable, :source_type => 'Post'
end
我的问题是,当我插入后成看点帖子的ID插入AspectVisibility作为邮政的关键。但实际上应该插入邮政的指导。
我见过的解决方案是这样的:
class Post < ActiveRecord::Base
set_primary_key :guid
[...]
end
但我并不想改变文章的外键一般,但只是为AspectVisibility关联。
有谁能告诉我该怎么做?
谢谢!
如果你在你的例子中发布所有模型的代码,它会有所帮助 – Tilo