0
我用Mongoid和Rails 3,和我有以下的單表繼承:在Mongoid中,如何排除基本控制器中的繼承模型對象?
class Post
include Mongoid::Document
field :title, type: String
field :content, type: String
end
有一個模型「第」從帖子繼承:
class Article < Post
field :source, type: String
end
我是新手嘗試STI 。我瞭解到「一個控制器」對於基礎和繼承模型來說是一個很好的設計。所以我有PostsController這樣
class PostsController < ApplicationController
def index
@type = param[:type] # type is passed from the route.rb
@posts = Post.where(_type: @type)
...
所以,如果被指定爲@type「項目」,@posts將只包含「文章」類型的職位。這在文章視圖中很好:只顯示文章,但不顯示其他類型的文章。
但是,在帖子視圖中,它將顯示帖子和文章。
我不希望文章顯示在我的帖子視圖中 - 實際上,我只想從視圖中顯示的基礎模型文章中看到帖子。有沒有一種方法可以從基礎控制器中的繼承模型中排除項目?
換句話說,我如何才能從基本模型中找到物品?
是的,否則它會返回一切。你可以嘗試在Post中添加一個default_scope(_type:「Post」),看看它是否有幫助。 – Roman