Rails 3.2中的活动记录查询问题

Man*_*han 7 passenger nginx active-record-query ruby-on-rails-3.2

我有类别模型和类别有很多帖子.

问题:有时在Web中的类别下不会显示过帐,即使数据库中存在记录

我通过启用config.log_level =调试并重新启动nginx乘客服务器来调查生产环境中的操作查询.现在我可以看到该类别下的记录.我无法再次重现同样的问题,很少发生.

注意:

  1. 我没有更改项目中的任何代码.相同的代码表现不同.
  2. Rails是3.2.22.Nginx乘客(5.1.1)

型号如下

class Category < ActiveRecord::Base
  has_many :postings, conditions: ['paid = ? AND start_date <= ? AND end_date >= ?', true, Date.current, Date.current]
end

class Posting < ActiveRecord::Base
  searchkick

  belongs_to :category

  class << self

    def payed
      where paid: true
    end

    def activated
     where :code => ""
    end

    def starts_on(date)
      where "start_date <= ?", date
    end

    def ends_after(date)
      where "end_date >= ?", date
    end

    def in_location(state,city)
      where(stateid: state.id, cityid: city.id)
    end

    def not_deleted
      where "active != false"
    end
end
Run Code Online (Sandbox Code Playgroud)

发布控制器

def index
  @category = Category.find(params[:category_id])
  postings = @category.postings.payed.activated.not_deleted.starts_on(Date.current).ends_after(Date.current).order(:created_at)
  @postings = postings.in_location(current_state, current_city).page(params[:page])
end
Run Code Online (Sandbox Code Playgroud)

从production.log,访问帖子页面/帖子?category_id = 25

类别加载(0.2ms)SELECT categories.*FROM categories WHERE categories.id= 25 LIMIT 1

(0.4ms)SELECT COUNT(*)FROM postingsWHERE postings.category_id= 25 AND postings.paid= 1 AND postings.code=''和postings.stateid= 44 AND postings.cityid= 14823 AND(活跃!=假)AND(付款= 1 AND listing_start_date <='2017-03-13'和listing_end_date> ='2017-03-13')AND(listing_start_date <='2017-03-13') AND(listing_end_date> ='2017-03-13')

CACHE(0. SELECT COUNT(*)FROM postingsWHERE postings.category_id= 25 AND postings.paid= 1 AND postings.code=''AND postings.stateid= 44 AND postings.cityid= 14823 AND(active!= false)AND(paid = 1 AND listing_start_date <='2017- 03-13'AND listing_end_date> ='2017-03-13')AND(listing_start_date <='2017-03-13')AND(listing_end_date> ='2017-03-13')

发布加载(0.4ms)SELECT postings.*FROM postingsWHERE postings.category_id= 25 AND postings.paid= 1 AND postings.code=''和postings.stateid= 44 AND postings.cityid= 14823 AND(活跃!=假)AND(付款= 1 AND listing_start_date <='2017-03-13'和listing_end_date> ='2017-03-13')AND(listing_start_date <='2017-03-13') AND(listing_end_date> ='2017-03-13')ORDER BY created_at LIMIT 10 OFFSET 0

上面的查询集没有选择任何记录; 启用调试模式并重启/触摸nginx服务器后,同一查询获取可用记录

问题是由活动记录查询/ Nginx /缓存引起的吗?

请帮我解决这个问题.

Man*_*han 2

Proc修复了使用关联条件的问题,例如

\n\n
has_many :postings, conditions: proc { "payed = 1 AND start_date <= \'#{Date.current.to_s(:db)}\' AND end_date >= \'#{Date.current.to_s(:db)}\'"}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您已与动态条件(如 )关联has_many :postings, conditions: [\'paid = ? AND start_date <= ? AND end_date >= ?\', true, Date.current, Date.current],则在某些情况下,您\xe2\x80\x99 将获得的结果与预期不同,因为条件将包含您启动 Rails 应用程序的日期,并且 Date.current 赢得\xe2\x80 \x99t 不会被再次调用。

\n\n

感谢何塞·吉尔加多。参考: http: //josemdev.com/articles/dynamic-conditions-associations-rails-3/

\n