-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
Labels:None
Setup:
class Parent
include Mongoid::Document
has_many_related :children
end
class Child
include Mongoid::Document
belongs_to_related :parent, :inverse_of => :children
field :tags, :type => Array
def self.tags
only(:tags).collect
end
end
Create a parent with a child:
Parent.create(:children => [Child.new(:tags => ["omg", "lol"])])
Calling the tags method on an association proxy doesn't work:
Parent.first.children.tags
NoMethodError: undefined method `merge' for ["omg", "lol"]:Array
from /Users/druiden/.rvm/gems/ruby-1.8.7-p249/bundler/gems/mongoid-7c52c9062e8404a8881d39f6d2b48690383a9f58-808358a093b7f920a1ab1810a3535091db212c66/lib/mongoid/criteria.rb:151:in `method_missing'
from /Users/druiden/.rvm/gems/ruby-1.8.7-p249/bundler/gems/mongoid-7c52c9062e8404a8881d39f6d2b48690383a9f58-808358a093b7f920a1ab1810a3535091db212c66/lib/mongoid/associations/references_many.rb:112:in `send'
from /Users/druiden/.rvm/gems/ruby-1.8.7-p249/bundler/gems/mongoid-7c52c9062e8404a8881d39f6d2b48690383a9f58-808358a093b7f920a1ab1810a3535091db212c66/lib/mongoid/associations/references_many.rb:112:in `method_missing'
from (irb):31
from :0
Doing it outside the class method works:
Parent.first.children.only(:tags).collect { |record| record.tags }
.flatten.uniq.compact
=> ["omg", "lol"]
Calling it from Child also works (but is not what I need):
Child.tags
=> ["omg", "lol"]