Search code examples
ruby-on-railssearchactiverecordransack

Rails ransack search condition


Is there a way to add a "condition" to a ransack search? I want to search through published Articles only.

Currently my code looks like this and is working perfectly. But it shows articles that are not yet published, too.

    @q = Article.ransack(params[:q])
    @q.sorts = 'name asc' if @q.sorts.empty?
    @article = @q.result(distinct: true).includes(:vendor,
                                               :description,
                                               :alt_art_names,
                                               :alt_vendor_names,
                                               :certificates,
                                               :article_reports,
                                               :article_ratings,
                                               :article_community_ratings)

Is there a way to add something like "where('article.published_at <= ?', Time.now)"?


Solution

  • Try doing:

     @q = Article.where("articles.published_at <= ?", Time.now).ransack(params[:q])
     ...