Rails 3.2 中的活动记录查询问题
Active record query issues in 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,当访问帖子页面/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
以上一组查询没有选取任何记录;在启用调试模式和 restart/touch 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 将不再被调用。
感谢何塞 M.Gilgado。参考:http://josemdev.com/articles/dynamic-conditions-associations-rails-3/
我有分类模型,分类有很多帖子。
问题: 有时即使数据库中有记录,也无法在 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,当访问帖子页面/postings?category_id=25
Category Load (0.2ms) SELECT
categories
.* FROMcategories
WHEREcategories
.id
= 25 LIMIT 1(0.4ms) SELECT COUNT(*) FROM
postings
WHEREpostings
.category_id
= 25 ANDpostings
.paid
= 1 ANDpostings
.code
= '' ANDpostings
.stateid
= 44 ANDpostings
.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
WHEREpostings
.category_id
= 25 ANDpostings
.paid
= 1 ANDpostings
.code
= '' ANDpostings
.stateid
= 44 ANDpostings
.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
.* FROMpostings
WHEREpostings
.category_id
= 25 ANDpostings
.paid
= 1 ANDpostings
.code
= '' ANDpostings
.stateid
= 44 ANDpostings
.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
以上一组查询没有选取任何记录;在启用调试模式和 restart/touch 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 将不再被调用。
感谢何塞 M.Gilgado。参考:http://josemdev.com/articles/dynamic-conditions-associations-rails-3/