rails converting an include query that returns nil into scope lambda -


the following query returning nil in instances

 scope :claim_one, where(" location_id null or start_time null or archived null " ).includes(:instructor_assignments).where('instructor_assignments.user_id null')  

i convert query lambda function takes care of nil returned query follows ;

scope :claim, (lambda   |claim| where(" location_id null or start_time null or archived null ").includes(:instructor_assignments).where('instructor_assignments.user_id null', claim ) unless claim.nil? end ) 

the latter not work don't think has correct rails syntax

thank

not sure if understand (see comment above), hope help. think results code activerecord::relation no records in rather nil. assuming "claim" parameter want pass in. if case, can add below.

the preferred way deal lambda scopes nicer now; use class method:

def self.claim_one(claim)   return [whatever want] unless claim   where(" location_id null or start_time null or archived null " ).     includes(:instructor_assignments).     where('instructor_assignments.user_id null') end 

i suggest following though make scopes easier use separately, easier understand , test, , darn purty:

def self.incomlete()   where(" location_id null or start_time null or archived null " ) end  def self.without_instructor()   includes(:instructor_assignments).where('instructor_assignments.user_id null') end 

to call this, use like:

result = yourmodel.incomplete.without_instructor if result.empty?   # yo funky "ain't got no records dance" else   # record jackpot!   end 

did answer question?


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -