假设我有两张 table ,Parent
和 Child
他们都有一个名为 name
的字段, 如果我做:Parent.join(:child).group(:name)
我会收到一条错误消息,指出该字段存在歧义 name
.我可以做到以下几点Parent.join(:child).group('parents.name')
为了解决它,但如果可能的话,我更喜欢使用符号。那么,是吗?
最佳答案
可以重新定义范围自动添加表名前缀
class Parent < ActiveRecord::Base
class << self; alias_method :old_group, :group; end
scope :group, lambda { |g|
old_group("#{table_name}.#{g}")
}
end
Parent.join(:child).group(:name)
关于ruby-on-rails - rails : solving ambiguity on group using symbols,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12646827/