2013-04-24 57 views
1

我明白你如何使用CLASS_NAME选项在Active Directory中提及的命名空间模式:如何在名称空间模型中使用多态关联?

has_one :slide, :class_name => '::Refinery::Slides::Slide' 

以及如何使用多态关联

has_one :slide, :as => :slideable 

你可以用它们在一起吗?

has_one :slide, :class_name => '::Refinery::Slides::Slide', :as => :slideable 

如果是这样,你如何定义多态关联?

belongs_to :slideable, :polymorphic => true, class_name='::Refinery::Slideables::Slideable' #NO 

我与RefineryCMS工作,每次添加引擎获取炼油厂:: PluralModel :: SingularModel命名空间。基本上,我希望能够将幻灯片与案例研究或工作相关联。这里是实际的模型。

module Refinery 
    module CaseStudies 
    class CaseStudy < Refinery::Core::BaseModel 
     attr_accessible :title, :description, :position 
     has_one :slide, :class_name => '::Refinery::Slides::Slide', :as => :slideable 
    end 
    end 
end 

module Refinery 
    module Works 
    class Work < Refinery::Core::BaseModel 
     attr_accessible :title, :description, :position, 
     has_one :slide, :class_name => '::Refinery::Slides::Slide', :as => :slideable 
    end 
    end 
end 

module Refinery 
    module Slides 
    class Slide < Refinery::Core::BaseModel 
     attr_accessible :slide_id, :caption, :position, :slideable_id, :slideable 
     belongs_to :slide, :class_name => '::Refinery::Image' 
     belongs_to :slideable, :polymorphic => true 
    end 
    end 
end 

好像我应该可以说slide.slideable.title,但我得到一个错误: 未定义的方法'标题”的零:NilClass

apidoc指定inverse_of无法与多态使用,但没有提及class_name

回答

0

要使多态关联有效,需要在另一个模型'belongs_to'上添加一个type列和一个id列。类型列将存储类名称,所以不需要在通用关联中指定。

例如,Comment可以属于QuestionAnswer。让我们把问题和答案称为“可评论的”。评论应该有:commentable_id:commentable_type的列。

class Comment < ActiveRecord::Base 
    # has columns :commentable_id and :commentable_type 
    belongs_to :commentable, :polymorphic => true 
end 

class Question < ActiveRecord::Base 
    has_many :comments, :as => :commentable 
end 

class Answer < ActiveRecord::Base 
    has_many :comments, :as => :commentable 
end 

# example 
comment = Comment.new(:body => "Nice answer!") 
comment.commentable = Answer.find(1) 
comment.save 

所以,你的情况,你可以从WorkCaseStudy删除:class_name选项,并确保您添加列:slide_type:slideable_type(去与:slideable_id)(与:slide_id去)你Slide。它应该全部“仅仅使用名称空间类名”。

module Refinery 
    module CaseStudies 
    class CaseStudy < Refinery::Core::BaseModel 
     attr_accessible :title, :description, :position 
     has_one :slide, :as => :slideable 
    end 
    end 
end 

module Refinery 
    module Works 
    class Work < Refinery::Core::BaseModel 
     attr_accessible :title, :description, :position, 
     has_one :slide, :as => :slideable 
    end 
    end 
end 

module Refinery 
    module Slides 
    class Slide < Refinery::Core::BaseModel 
     attr_accessible :slide_id, :caption, :position, :slideable_id, :slideable 
     # add :slide_type and :slideable_type columns 
     belongs_to :slide, :polymorphic => true 
     belongs_to :slideable, :polymorphic => true 
    end 
    end 
end 
+0

谢谢你这么彻底的回答!我对能够删除class_name持怀疑态度,但现在呢 - 会回来并让你知道。 – tim 2013-04-24 12:41:31

+0

它看起来像你说得对,我可以删除CLASS_NAME因为在我的形式我设置slideable_type这样 slide.radio_button(:slideable_type,炼油厂::作品::工作,:检查=>(:slideable_type == '作品')) – tim 2013-04-24 13:32:32

相关问题