我尝试按照命名函数的结果进行分组时获得NoMethodError: undefined method relation' for <Arel::Nodes::NamedFunction:0x7633>
。
class Appointment < ActiveRecord::Base
scope :group_by_date, -> { group(date_column) }
def self.date_column
Arel::Nodes::NamedFunction.new(:date, [Appointment.arel_table[:start_time]], 'date')
end
end
Appointment.group_by_date.count
# results in NoMethodError: undefined method `relation' for #<Arel::Nodes::NamedFunction:0x7633>
这看起来非常合理,所以我不确定为什么它会造成错误。我很确定在this SO answer所示的早期版本的rails中这是可能的。
我希望得到类似于以下sql的内容:
SELECT date(appointments.start_time) AS date, COUNT(appointments.*) AS count
FROM appointments
GROUP BY date(appointments.start_time)
有没有办法让它发挥作用?有没有办法将Arel::Nodes::NamedFunction
转换为Arel::Attributes::Attribute
这显然是对解决方案的天真尝试(它没有奏效):
function = Arel::Nodes::NamedFunction.new('date', [Appointment[:start_time]])
attr = Arel::Attributes::Attribute.new(function)
Appointment.group(attr).to_sql
# NoMethodError: undefined method `table_alias' for #<Arel::Nodes::NamedFunction:0x4b8>
我真的不想放弃使用Appointment.group( 'date(appointments.start_time)' )
,因为我做了大量的范围合并和加入以及字符串造成严重破坏,因为他们并不总是适用于正确的表格
环境:rails(4.1.8),activerecord(4.1.8),arel(5.0.1.20140414130214)
答案 0 :(得分:2)
这是一个棘手的问题。首先,你的NamedFunction定义有几个完全不明显的问题:首先,第一个参数需要是一个字符串,否则你得到这个:
irb(main):040:0> User.group( Arel::Nodes::NamedFunction.new(:date, [User.arel_table[:created_at]], 'date') )
TypeError: no implicit conversion of Symbol into String
from /var/lib/gems/2.1.0/gems/arel-6.0.2/lib/arel/collectors/plain_string.rb:13:in `<<'
from /var/lib/gems/2.1.0/gems/arel-6.0.2/lib/arel/visitors/to_sql.rb:458:in `visit_Arel_Nodes_NamedFunction'
其次,你需要删除第三个参数(别名),否则Arel会尝试将它推入GROUP BY子句并且你得到一个错误(至少在Postgres中):
irb(main):045:0> User.group( Arel::Nodes::NamedFunction.new('date', [User.arel_table[:created_at]], 'date') )
User Load (4.9ms) SELECT "users".* FROM "users" GROUP BY date("users"."created_at") AS date
PG::SyntaxError: ERROR: syntax error at or near "AS"
LINE 1: ...".* FROM "users" GROUP BY date("users"."created_at") AS date
^
: SELECT "users".* FROM "users" GROUP BY date("users"."created_at") AS date
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "AS"
LINE 1: ...".* FROM "users" GROUP BY date("users"."created_at") AS date
^
: SELECT "users".* FROM "users" GROUP BY date("users"."created_at") AS date
from /var/lib/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:596:in `async_exec'
你没有注意到这些,因为你得到的错误实际上与.count
电话有关;没有它,这实际上现在有效:
irb(main):066:0> nf = Arel::Nodes::NamedFunction.new('date', [User.arel_table[:created_at]])
=> #<Arel::Nodes::NamedFunction:0x9dc9ca0 @expressions=[#<struct Arel::Attributes::Attribute relation=#<Arel::Table:0xbdb689c @name="users", @engine=User(id: integer, name: string, age: integer, created_at: datetime, updated_at: datetime), @columns=nil, @aliases=[], @table_alias=nil, @primary_key=nil>, name=:created_at>], @alias=nil, @distinct=false, @name="date">
irb(main):067:0> User.group(nf).select(nf)
User Load (2.5ms) SELECT date("users"."created_at") FROM "users" GROUP BY date("users"."created_at")
=> #<ActiveRecord::Relation [#<User id: nil>]>
那么,为什么.count
不起作用?不幸的是,在堆栈跟踪之后,这看起来就像一个bug。 count
方法进入ActiveRelation::Calculations#execute_grouped_calculation并且只是走错路。在那里没有支持从Arel处理NamedFunction。
但是,你可以解决它:
irb(main):017:0> User.group(nf).pluck('count(*)').first
(2.5ms) SELECT count(*) FROM "users" GROUP BY date("users"."created_at")
=> 1
所以......是的您可能想在Rails中为此打开一个问题。我个人建议你按字符串表达式分组并省去一些头痛!