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

前端之家收集整理的这篇文章主要介绍了nginx – Rails 3.2中的活动记录查询问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

注意:

>我没有更改项目中的任何代码.相同的代码表现不同.
> 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

发布控制器

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

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

Category Load (0.2ms) SELECT categories.* FROM categories
WHERE categories.id = 25 LIMIT 1

(0.4ms) SELECT COUNT(*) FROM postings WHERE
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’)

CACHE (0. SELECT COUNT(*) FROM postings WHERE
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’)

Posting Load (0.4ms) SELECT postings.* FROM postings WHERE
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’) ORDER BY created_at LIMIT 10 OFFSET
0

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

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

请帮我解决这个问题.

最佳答案
修复了使用Proc进行关联条件的问题

has_many :postings,conditions: proc { "payed = 1 AND start_date <= '#{Date.current.to_s(:db)}' AND end_date >= '#{Date.current.to_s(:db)}'"}

如果你与动态条件如has_many:postss,条件:[‘paid =? AND start_date< =? AND end_date> =?’,Date.current],有些情况下你不会得到你得到的结果,因为条件将有你启动Rails应用程序的日期和Date.current赢了不能再打电话了.

感谢Jose M.Gilgado.参考:http://josemdev.com/articles/dynamic-conditions-associations-rails-3/

原文链接:https://www.f2er.com/nginx/434834.html

猜你在找的Nginx相关文章