加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 21:01:56 所属栏目:Nginx 来源:网络整理
导读:我有类别模型和类别有很多帖子. 问题:有时在Web中的类别下不会显示过帐,即使数据库中存在记录 我通过启用config.log_level =:debug并重新启动了来调查生产环境中的操作查询 nginx乘客服务器.现在我可以看到该类别下的记录.我无法再次重现同样的问题,很少发

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

问题:有时在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/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读