gpt4 book ai didi

nginx - Rails 3.2 中的事件记录查询问题

转载 作者:行者123 更新时间:2023-12-04 12:45:54 25 4
gpt4 key购买 nike

我有类别模型,类别有很多帖子。

问题:有时,即使数据库中存在记录,在网络类别下也看不到帖子

我通过启用 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

    过帐 Controller
    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,访问帖子页面时/postings?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



    上述查询集没有选择任何记录;在启用 Debug模式并重新启动/触摸 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 :postings, conditions: ['paid = ? AND start_date <= ? AND end_date >= ?', true, Date.current, Date.current] ,有些情况下你会得到意想不到的结果,因为条件是你启动 Rails 应用程序的那一天,并且 Date.current 不会再次被调用。

    感谢 Jose M.Gilgado。引用: http://josemdev.com/articles/dynamic-conditions-associations-rails-3/

    关于nginx - Rails 3.2 中的事件记录查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42760228/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com