Search code examples
ruby-on-railselasticsearchscopesearchkick

How to use scopes with searchkick in controller?


Here is my code in my controller index. For @posts I was only showing published posts and posts where the user didn't favorite, rsvp, etc. When I add the search for searchkick, it ignores the other conditions. the search works but it includes unpublished posts, etc. How can I make adjusts in the model or controller to be able to search and limit my query.

@favorites = current_user.favorites
      @rsvps = current_user.rsvps
      @unshows = current_user.unshows

      query = params[:q].presence || "*"
      @posts = Post.published.where.not(id: @unshows).where.not(id: @favorites).where.not(id: @rsvps).search(query)

Here is my model.

class Post < ActiveRecord::Base
    belongs_to :user
    default_scope -> { order(created_at: :desc) }

    searchkick text_start: [:title]

    def search_data
        {
            title: title,
            description: description,
            location: location,
            date: date

        }
    end

    validates :user_id, presence: true
    validates_presence_of :slug

    has_many :repluggers, :class_name => 'user', :foreign_key => 'user_id'
    has_many :replugs, :class_name => 'post', :foreign_key => 'replug_id'

    has_attached_file :image, :styles => { :medium => "800x800#", :thumb => "100x100#" }
    validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

    # Favorited by users
    has_many :favorite_posts # just the 'relationships'
    has_many :favorited_by, through: :favorite_posts, source: :user

    # Favorited by users
    has_many :rsvp_posts # just the 'relationships'
    has_many :rsvp_by, through: :rsvp_posts, source: :user

    # Favorited by users
    has_many :unshow_posts # just the 'relationships'
    has_many :unshow_by, through: :unshow_posts, source: :user

    scope :draft, ->{where(published_at: nil)}
    scope :published, ->{where.not(published_at: nil).where("published_at <= ?", Time.now.in_time_zone("Eastern Time (US & Canada)"))}
    scope :scheduled, ->{where.not(published_at: nil).where("published_at >= ?", Time.now.in_time_zone("Eastern Time (US & Canada)"))}



     attr_accessor :status

     before_validation :clean_up_status

     def clean_up_status
        self.published_at = case status
                            when "Draft"
                                nil
                            when "Published"
                                Time.zone.now
                            else
                                published_at
                            end
        true
     end

    def slug
        title.to_s.downcase.strip.gsub(" ", "-").gsub(/[^\w-]/, '') 
    end

    def to_param
        "#{id}-#{slug}"
    end
end

Solution

  • // query = search query term

    // @rsvps, @favorites, @unshows => array of post_ids

    Post.search query, 
              where: {
                 published_at: {lte: Time.now.in_time_zone("Eastern Time (US & Canada)")},
                 id: {not: @favorites},
                 id: {not: @rsvps},
                 id: {not: @unshows}
              }