首页 > 解决方案 > 如何将其他同名和 date_posted 的帖子计算为当前帖子?

问题描述

我在 ruby​​ on rails 中有这个应用程序,其中我想在保存当前 po 之前计算所有具有相同标题和日期的帖子作为当前帖子发布。

在我的后控制器创建方法中,我正在做类似的事情 -

@post = Post.new(post_params)
# @post.datepublished = it will be today's date
totalnoofposts = Post.count(:all, :conditions => "title = " @post.title " AND datepublished = " @post.datepublished)

# then I'll save the new post

我遇到了一个很长的语法错误,谁能帮助我如何以这种方式计算这些帖子?

标签: ruby-on-railsruby-on-rails-5

解决方案


尝试这个

Post.where(title: @post.title, datePublished: @post.datePublished).count

还要注意一些事情。RoR 中的约定是,字段为蛇形,即 date_published,而不是 datePublished。以及在问题中包含错误消息。


推荐阅读