Mysql2 Error in activerecord query
<p>Hi all getting this error and I think it's appended to this query.</p>

<pre><code>ERROR -- : Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use 
near '? AND `profile_activity`.`entity_type` IN (?, ?, ?, ?)  ORDER BY `profile_activi' at line 1: 

   EXPLAIN SELECT  `profile_activity`.`date_created`, `profile_activity`.`entity_id`, `profile_activity`.`entity_type`, `profile_activity`.`activity_type` 
   FROM `profile_activity` 
   WHERE `profile_activity`.`user_id` = 197056 
       AND `profile_activity`.`activity_type` = ? 
       AND `profile_activity`.`entity_type` IN (?, ?, ?, ?)  
   ORDER BY `profile_activity`.`date_created` DESC 
   LIMIT 19 OFFSET 0
</code></pre>

<p>Here is the active record query</p>

<pre><code>    ProfileActivity
      .select(:date_created, :entity_id, :entity_type, :activity_type)
      .where(activity_type: 'V', entity_type: ['A', 'B', 'C', 'WL'])
      .order(date_created: :desc)
      .limit(limit)
      .offset(offset)
</code></pre>

<p>It looks like it does not interpret the first string 'V' and so forth. Not sure why this is happening. Thanks for any help with this.</p>

<p>The database columns are CHARS for entity_type and activity_type.</p>

<p>EDIT***</p>

<p>profile activity model</p>

<pre><code>class ProfileActivity &lt; ActiveRecord::Base
  self.table_name = 'profile_activity'

  belongs_to :user
  has_one :agency, foreign_key: :agency_id, primary_key: :entity_id
  has_one :company, foreign_key: :company_id, primary_key: :entity_id
  has_one :company_brand, foreign_key: :brand_id, primary_key: :entity_id

  scope :views, -&gt; (limit, offset) {
  select(:date_created, :entity_id, :entity_type, :activity_type).where(activity_type: 'V', entity_type: ['A', 'B', 'C', 'WL']).order(date_created: :desc).limit(limit).offset(offset)
  }

  scope :most_recent_views_by_user, -&gt; (user_id) {
    sql        = ProfileActivity.where(user_id: user_id).to_sql
    table_name = ProfileActivity.table_name
    select(:date_created, :entity_id, :entity_type, :activity_type)
.where(activity_type: 'V', entity_type: ['A', 'B', 'C'])
.order(date_created: :desc).limit(10)
.from("(#{sql}) as #{table_name}")
    }
end
</code></pre>

<p>my_activity_controller.rb</p>

<pre><code>def user_activities
activities = []

if current_user
  decoder        = HTMLEntities.new
  activity_views = ProfileActivity.where(user_id: current_user.id).views(params[:limit], params[:offset])

  activity_views.each do |a|
    next if (a.created_at.nil? || a.entity_id == 0)

    begin
      case a.entity_type
      when 'B'
        business            = Business.find_by(id: a.entity_id)
        entity_name         = decoder.decode(business.name)
        entity_type         = 'Business'
        entity_abbreviation = 'B'
      else
        entity_name = nil
        entity_type = nil
      end

      case a.activity_type
      when 'D'
        activity_type = 'downloaded'
      when 'P'
        activity_type = 'printed'
      when 'V'
        activity_type = 'viewed'
      else
        activity_type = nil
      end

      case a.entity_type
      when 'B'
        url = business_profile_path(a.entity_id)
      else
        url = nil
      end

      activities &lt;&lt; { date:                a.created_at,
                      url:                 url,
                      entity_name:         entity_name,
                      entity_type:         entity_type,
                      entity_abbreviation: entity_abbreviation,
                      activity_type:       activity_type }
    rescue =&gt; e
      ErrorHandler.notify_exception(e, env: env) rescue nil
    end
  end
end
</code></pre>