![](/img/trans.png)
[英]Rails API makes multiple database queries when similar code in Rails console only makes one database query
[英]Rails console : query to database
我有一个模型报表,其中定义了这些列:
class Report < ActiveRecord::Base {
:id => :uuid,
:declarant_id => :uuid,
:reference => :integer,
:sent_at => :datetime,
:updated_by_id => :uuid,
:device_id => :string,
:created_at => :datetime,
:updated_at => :datetime,
:is_archived => :boolean,
:step_id => :uuid
}
我想过滤选择查询以仅获取7天前创建的报告,所以我定义了该查询与其他一些输入和方法的关系:
r = Report.all.current.not_sent.only_finished_interventions.where("Report.created_at >= ?", 7.days.ago)
它行不通!
当我做 :
r = Report.all.current.not_sent.only_finished_interventions
我得到了要求的报告。
有人可以帮助添加7.days.ago的过滤器吗?
我在rails c中收到此错误:
ActiveRecord :: StatementInvalid:PG :: UndefinedTable:错误:缺少表“报告”第1行的FROM子句条目:... AND“活动”。“ finished_at”不为空)AND(Report.cre ...
这应该工作:
.where("Reports.created_at >= ?", 7.days.ago)
只需使Report为复数即可。
但是我认为报告。 不连接表的查询bcs中没有必要,因此这也应该起作用:
**`.where("created_at >= ?", 7.days.ago)`**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.