Les Rails .où() attribut n'EST PAS NULL
J'ai un Page
modèle contenant de nombreux Section
modèles qui est associée à une SectionRevision
par current_revision
. À partir de la Page
modèle que je suis en train de sélectionner tous les Sections
où la current_revision.parent_section_id
n'est pas nul.
Section
modèle:
class Section < ActiveRecord::Base
belongs_to :page
has_many :revisions, :class_name => 'SectionRevision', :foreign_key => 'section_id'
has_many :references
has_many :revisions, :class_name => 'SectionRevision',
:foreign_key => 'section_id'
belongs_to :current_revision, :class_name => 'SectionRevision', :foreign_key => 'current_revision_id'
delegate :position, to: :current_revision
def set_current_revision
self.current_revision = self.revisions.order('created_at DESC').first
end
def children
Section.includes(:current_revision).where(:section_revisions => {:parent_section_id => self.id})
end
end
Et Page
modèle:
class Page < ActiveRecord::Base
belongs_to :parent, :class_name => 'Page', :foreign_key => 'parent_page_id'
has_many :children, :class_name => 'Page', :foreign_key => 'parent_page_id'
belongs_to :page_image, :class_name => 'Image', :foreign_key => 'page_image_id'
has_many :sections
validates_uniqueness_of :title, :case_sensitive => false
def top_level_sections
self.sections.includes(:current_revision).where(:section_revisions => {:parent_section_id => "IS NOT NULL"})
end
end
Page.top_level_sections
est écrit basé sur: Rails où, à l'aide not NULL et produit actuellement un tableau vide. Il n'est pas détecter correctement si "parent_section_id" n'est pas nulle.
Comment puis-je écrire Page.top_level_sections
correctement?
- Quelle est l'intention de
Page.top_level_sections
? Est-il en essayant de trouver des articles qui n'ont pas de révisions? - En essayant de trouver des sections où current_revision.parent_section_id n'est pas nul.
Vous devez vous connecter pour publier un commentaire.
Essayez ceci:
sections.includes(:current_revision).where.not(section_revisions: {parent_section_id: nil})