Rails 3.2中的活动logging查询问题

我有类别模型和类别有很多post。

问题:有时post在networking中的类别下是不可见的,即使logging存在于数据库中

我通过启用config.log_level =:debug来调查查询生产环境中的操作,并重新启动了nginx客运服务器。 现在我可以看到类别下的logging。 我无法再次重现相同的问题,而且很less发生。

注意:

  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 

发布控制器

 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 WHERE categoriesid = 25限制1

(0.4ms)selectCOUNT(*)FROM postings WHERE postingscategory_id = 25 AND postingspaid = 1和postingscode =''和postingsstateid = 44和postings 。 AND(listing_start_date <='2017-03-13')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 = 25 AND postings = 1 AND postings =''AND postings = 44 AND postings = 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 *从过postings哪里过postingscategory_id = 25 AND postingspaid = 1和postingscode =''和postingsstateid = 44和postings 。 AND(listing_start_date <='2017-03-13')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

上述一组查询没有select任何logging; 并启用debugging模式后,重新启动/触摸nginx服务器相同的查询获取可用的logging

是由活动logging查询/ Nginx /caching引起的问题?

请帮我解决这个问题。

修正了使用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]那样完成动态条件的关联has_many :postings, conditions: ['paid = ? AND start_date <= ? AND end_date >= ?', true, Date.current, Date.current] has_many :postings, conditions: ['paid = ? AND start_date <= ? AND end_date >= ?', true, Date.current, Date.current] has_many :postings, conditions: ['paid = ? AND start_date <= ? AND end_date >= ?', true, Date.current, Date.current] ,将会出现你不会得到结果的情况,因为这个条件会有你启动Rails应用程序的日子,Date.current won'再次被调用。

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