Rails: eliminate ambiguity in a group using characters

Let's say I have two tables, Parentand Child, and both of them have a field called nameif I do this:

Parent.join(:child).group(:name)

I will get an error stating that namethere is uncertainty in the field . I could do the following

Parent.join(:child).group('parents.name')

To solve this problem, but I would prefer to use a symbol if possible. So what is this?

+5
source share
1 answer

You can override the scope to automatically add a table name prefix

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)
+1
source

All Articles