model - Rails .where() attribute IS NOT NULL -


i have page model containing many section models associated sectionrevision through current_revision. page model trying select sections current_revision.parent_section_id not nil.

section model:

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 

and page model:

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 written based on: rails condition using not null , produces empty array. doesn't correctly detect if "parent_section_id" not null.

how write page.top_level_sections correctly?

try this:

self.sections.includes(:current_revision).   where("section_revisions.parent_secti‌​on_id not null") 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -