【发布时间】:2016-12-30 16:56:02
【问题描述】:
我有一个方便的范围,includes 相关模型可以加快表格的渲染等:
class Post < ApplicationRecord
...
scope :includes_for_post_row, -> { includes(:reasons).includes(:feedbacks => [:user]) }
它工作正常。但是,现在我想select 一个附加属性。如果我已经知道我想要什么初始属性,我可以这样做(在控制台中):
2.3.3 :005 > Post.select("`posts`.*, 42 AS column_forty_two").last.column_forty_two
Post Load (1.0ms) SELECT `posts`.*, 42 AS column_forty_two FROM `posts` ORDER BY `posts`.`id` DESC LIMIT 1
=> 42
这假设我知道我想选择posts.*,然后我只需添加我的column_forty_two 列,一切正常。
我想将column_forty_two 添加到我的结果中,而不影响初始选择。例如,这应该有效:
p = Post.select("`posts`.*, 8 as column_eight").includes_for_post_row_with_forty_two
p.last.column_forty_two # => 42
p.last.column_eight # => 8
p.last.some_activerecord_property # => value
应该这样:
p = Post.all.includes_for_post_row_with_forty_two.last
p.last.column_forty_two # => 42
p.last.some_activerecord_property # => value
我如何select 一个附加 列,而不影响或覆盖.all 或我之前的select 默认选择的现有列?
【问题讨论】:
标签: ruby-on-rails activerecord rails-activerecord