0

I have a couple mongoid models:

class Album
  include Mongoid::Document
  field :name, type: String
  embedded_in :band
end
class Band
  include Mongoid::Document
  field :name, type: String
  embeds_many :albums
end

And I'm trying to get inherited_resources to include the embedded albums in the json for bands, like so:

class BandsController < InheritedResources::Base
  respond_to :html, :xml, :json
  def permitted_params
    params.permit!
  end
protected
  def collection
    @bands ||= end_of_association_chain.includes(:albums)
  end
end

But I get the following error when trying to retrieve the list of bands:

undefined method `eager_load' for Mongoid::Relations::Embedded::Many:Class

Any idea what I might be doing wrong?

cayblood
  • 1,838
  • 1
  • 24
  • 44
  • 1
    You don't have to use `includes(:albums)` because your albums are already embedded in their respective band. Therefore, retrieving a band will also retrieve all its albums at the same time. See it as a book, if you buy a book at the store, the pages are already inside, you don't have to specify that you want the book with its pages. – Raindal Oct 01 '13 at 16:47
  • Thanks Sparda, but when I retrieve the json collection of my bands, I'd like the collection to include nested json of the albums. At the moment, the embedded albums don't show up in the json. Any idea how I do that? – cayblood Oct 02 '13 at 17:14

1 Answers1

2

I am pretty much sure the error here is because you are overwriting collection method. collection is an internal method that mongoid uses to do operations in the collection, so I guess if you overwrite it would cause some conflicts.

Arthur Neves
  • 11,840
  • 8
  • 60
  • 73