Ruby on rails ActiveRecord方法调用优化
我有一段代码,看起来像这样:Ruby on rails ActiveRecord方法调用优化,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有一段代码,看起来像这样: Post.all.reject {|p| p.created_at.beginning_of_month != params[:date].to_date} 是否有一种方法可以使用where方法编写相同的代码,而不获取所有元素 如果您想使用where,我会选择: # x-month being a date from your desired month. # .. defines the range between the beginning and the e
Post.all.reject {|p| p.created_at.beginning_of_month != params[:date].to_date}
是否有一种方法可以使用where方法编写相同的代码,而不获取所有元素 如果您想使用
where
,我会选择:
# x-month being a date from your desired month.
# .. defines the range between the beginning and the end
Post.where(:created_at => x-month.beginning_of_month..x-month.end_of_month)
如果您想使用
where
,我会选择:
# x-month being a date from your desired month.
# .. defines the range between the beginning and the end
Post.where(:created_at => x-month.beginning_of_month..x-month.end_of_month)
顺便说一句,没有数据库不可知的解决方案,因为您需要从日期中提取月份。因此,在原始SQL中,您将有:
date = params[:date].to_date
Post.where("MONTH(created_at) != ? AND YEAR(created_at) = ?", [date.month, date.year])
现在,为了使用db不可知的解决方案,可以通过规范化进行一些欺骗。
只需在您的模型中添加一些created_at_month
和created_at_year
列,以及以下回调:
after_create :denormalize_created_at
def denormalize_created_at
assign_attributes created_at_month: created_at.month,
created_at_year: created_at.year
save validate: false
end
现在您可以执行以下操作:
轨道<4:
date = params[:date].to_date
Post
.where(Post.arel_table[:created_at_month].not_eq date.month)
.where(created_at_year: date.year)
Rails 4+:
date = params[:date].to_date
Post.not(created_at_month: date.month).where(created_at_year: date.year)
顺便说一句,没有数据库不可知的解决方案,因为您需要从日期中提取月份。因此,在原始SQL中,您将有:
date = params[:date].to_date
Post.where("MONTH(created_at) != ? AND YEAR(created_at) = ?", [date.month, date.year])
现在,为了使用db不可知的解决方案,可以通过规范化进行一些欺骗。
只需在您的模型中添加一些created_at_month
和created_at_year
列,以及以下回调:
after_create :denormalize_created_at
def denormalize_created_at
assign_attributes created_at_month: created_at.month,
created_at_year: created_at.year
save validate: false
end
现在您可以执行以下操作:
轨道<4:
date = params[:date].to_date
Post
.where(Post.arel_table[:created_at_month].not_eq date.month)
.where(created_at_year: date.year)
Rails 4+:
date = params[:date].to_date
Post.not(created_at_month: date.month).where(created_at_year: date.year)
mysql有一个
MONTH
函数来获取datetime列的月份
Post.where("MONTH(created_at) != ?", params[:date].to_date.month)
mysql有一个
MONTH
函数来获取datetime列的月份
Post.where("MONTH(created_at) != ?", params[:date].to_date.month)
你的rails版本是什么?你的数据库呢?最重要的是,你想要实现什么?rails 3.2.14和MySQL如果我理解的很好,你想要选择所有不是在某个月创建的帖子?是的,绝对正确rails版本是什么?你的数据库呢?最重要的是,你想实现什么?rails 3.2.14和MySQL如果我理解的很好,你想选择所有不是在某个月创建的帖子?是的,绝对正确+1-嘿,很好的解决方案。实际上,你必须使用
而不是来匹配OP的期望。你是对的。。。而这只与rails 4一起提供。。。哈哈,那么我们必须再次使用sqlwhere(“…”
):/rails<4中的arel是可能的,请参阅我的答案+1-嘿,很好的解决方案。实际上,你必须使用而不是来匹配OP的期望。你是对的。。。而这只与rails 4一起提供。。。哈哈,那么我们必须再次使用sqlwhere(“…”
):/rails<4中的arel是可能的,请参阅我的答案如果您更经常需要这些查询,这是一个很好的解决方法。对于一次性使用,设置起来有点太多了。:)是的。只是我倾向于选择DB不可知的解决方案。。。如果在这些列上有索引,它有时会表现得更好。如果您更经常地需要这些查询,这是一个很好的解决方法。对于一次性使用,设置起来有点太多了。:)是的。只是我倾向于选择DB不可知的解决方案。。。我认为这是在MySQL中包含NOT的最佳解决方案我认为这是在MySQL中包含NOT的最佳解决方案