Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Date.today in joins part of named scope

For some reason, the Date class is recognized when I use in the where

scope :with_active_subthing, joins("left outer join thing_subthing on thing_subthing.patient_id = thing.id").where("thing_subthing.start_date <= ? AND ( thing_subthing.end_date > ? OR thing_subthing.end_date IS NULL ) ", Date.today, Date.today)

but doesn't recognize it when I use it in joins

scope :with_active_subthing, joins("left outer join thing_subthing on thing_subthing.patient_id = thing.id AND (thing_subthing.start_date <= ? AND ( thing_subthing.end_date > ? OR thing_subthing.end_date IS NULL )) ", Date.today, Date.today)

I get:

RuntimeError: unknown class: Date
like image 405
Brandon Hamilton Avatar asked Oct 24 '25 16:10

Brandon Hamilton


1 Answers

If you look at the underlying code that builds the join portion, you can see that it is looking for specific object types, and Date is not one of them. The error is sort of confusing, but what it's saying is that a Date cannot be used as an argument for joins. It's not really clear from your question why you want to do this, but you should absolutely be using both joins and where together as you've done in your first example.

Here's the code that is causing the issue because it's falling through to the "raise" line:

def build_joins(manager, joins)
  buckets = joins.group_by do |join|
    case join
    when String
      'string_join'
    when Hash, Symbol, Array
      'association_join'
    when ActiveRecord::Associations::JoinDependency::JoinAssociation
      'stashed_join'
    when Arel::Nodes::Join
      'join_node'
    else
      raise 'unknown class: %s' % join.class.name
    end
  end
like image 52
Peter Brown Avatar answered Oct 26 '25 06:10

Peter Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!