Datamapper stores objects in a hash so they don't get created multiple
times per request (see IdentityMap). It's a performance thing, so I'm
guessing that your other Ingredients are there because it wouldn't
make sense for DM to return the same thing twice. You can get the
results you want by doing this:
@ingredients = Menu.first( :name => 'main').meals.collect{ |m|
m.ingredients }.flatten
That won't affect the performance much because DM will eager load the
'ingredients' for you, but it will still give you an array of what you
need (or an array of array's without the #flatten)
On Apr 30, 7:29 am, "J.D." <[email protected]> wrote:
> I'm new to DataMapper and trying my best to learn and embrace it
> instead of resorting to SQL. In this particular situation I have a
> model with Menus, Meals and Ingredients. All set up with "has
> n, :XXX, :through => Resource".
>
> In one location I want to grab a Menu, and show all ingredients for
> all meals in that menu. The problem is if 2 meals reference "1 onion",
> for example. In that case DM is generating a join that only provides 1
> entry for "1 onion", but I expect two.
>
> I'm using:
>
> @ingredients = Menu.first( :name => 'main').meals.ingredients.all
>
> I'm assuming I need to somehow modify my reference to the meals
> collection, but I can't seem to find where the documentation talks
> about this. Any pointers?
>
> J.D.
>
> Model for reference:
>
> class Meal
> include DataMapper::Resource
>
> property :id, Serial # autoinc
> <snip>
>
> has n, :ingredients, :through => Resource
> has n, :menus, :through => Resource
> end
>
> class Ingredient
> include DataMapper::Resource
>
> property :id, Serial
> property :name, String
> <snip>
>
> has n, :meals, :through => Resource
> end
>
> class Menu
> include DataMapper::Resource
>
> property :id, Serial
> property :name, String
>
> has n, :meals, :through => Resource
> end
--
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en.