Rails 3.2

时间:2017-03-13 09:28:34

标签: nginx ruby-on-rails-3.2 passenger active-record-query

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

问题:有时在网络中的类别下无法显示帖子,即使数据库中存在记录

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

注意:

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

    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

      

    类别加载(0.2ms)SELECT categories。* FROM categories   在categoriesid = 25 LIMIT 1

         

    (0.4ms)SELECT COUNT(*)FROM postings WHERE   postingscategory_id = 25 AND postingspaid = 1 AND   postingscode =''和postingsstateid = 44 AND   postingscityid = 14823 AND(有效!= false)AND(有效= 1 AND   listing_start_date&lt; ='2017-03-13'和listing_end_date&gt; =   '2017-03-13')AND(listing_start_date&lt; ='2017-03-13')AND   (listing_end_date&gt; ='2017-03-13')

         

    CACHE(0. SELECT COUNT(*)FROM postings WHERE   postingscategory_id = 25 AND postingspaid = 1 AND   postingscode =''和postingsstateid = 44 AND   postingscityid = 14823 AND(有效!= false)AND(有效= 1 AND   listing_start_date&lt; ='2017-03-13'和listing_end_date&gt; =   '2017-03-13')AND(listing_start_date&lt; ='2017-03-13')AND   (listing_end_date&gt; ='2017-03-13')

         

    发布加载(0.4ms)SELECT postings。* FROM postings WHERE   postingscategory_id = 25 AND postingspaid = 1 AND   postingscode =''和postingsstateid = 44 AND   postingscityid = 14823 AND(有效!= false)AND(有效= 1 AND   listing_start_date&lt; ='2017-03-13'和listing_end_date&gt; =   '2017-03-13')AND(listing_start_date&lt; ='2017-03-13')AND   (listing_end_date&gt; ='2017-03-13')ORDER BY created_at LIMIT 10 OFFSET   0

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

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

    请帮我解决此问题。

1 个答案:

答案 0 :(得分:2)

修复了使用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/