Storing of reference id in association

183 views
Skip to first unread message

Arjen

unread,
Oct 21, 2010, 12:59:20 PM10/21/10
to Mongoid
I've been using Mongoid with Rails 3 for a few weeks now. I have a
question though regarding storing of the reference id in an
association. Let me give you an example:

class Product
include Mongoid::Document
referenced_in :groups
end

class Group
include Mongoid::Document
references_many :products, :stored_as => Array, :inverse_of
=> :group
end

g = Group.create
p = Product.create

g.products << p
g.save

Now I would expect the id's of the products being stored (in the
database) in the group as product_ids (as an array) but the id of the
group is stored in the product as group_id. This, in my opinion, is
not working as expected. Am I doing something wrong, or is this the
way Mongoid does the assoication?

Regards,
Arjen

Joel

unread,
Oct 23, 2010, 11:44:20 AM10/23/10
to Mongoid
I got hung up on this too. I think the combination of how it's
described in the model (references_many) combined with the mental/
visual representation of embeds_many can help get things a bit
twisted. After you see it working though it makes sense - it's
essentially the same way activerecord works with has_many and belongs
to - the parent id is stored with the child ... and not the child id's
being stored within the parent.

As an exercise to remind myself of this down the line (perhaps to just
force my brain to get it right) I just put up a blog post with some
ramblings on how it ends up within the database.

http://joeloliveira.com/2010/10/23/visualizing-the-difference-between-references_many-and-embeds_many-in-mongoid/

Hope this helps.

- Joel

Arjen

unread,
Oct 24, 2010, 7:48:19 AM10/24/10
to Mongoid
Thank you Joel. It's good to know that it's working as designed and
that I'm not doing it the wrong way. It's hard to visualize the
relation being setup as it is. In all fairness the relations are
working peachy, so I should not dwell on it.

Thanks again.

On Oct 23, 5:44 pm, Joel <joel.olive...@gmail.com> wrote:
> I got hung up on this too.  I think the combination of how it's
> described in the model (references_many) combined with the mental/
> visual representation of embeds_many can help get things a bit
> twisted.   After you see it working though it makes sense - it's
> essentially the same way activerecord works with has_many and belongs
> to - the parent id is stored with the child ... and not the child id's
> being stored within the parent.
>
> As an exercise to remind myself of this down the line (perhaps to just
> force my brain to get it right) I just put up a blog post with some
> ramblings on how it ends up within the database.
>
> http://joeloliveira.com/2010/10/23/visualizing-the-difference-between...
>
> Hope this helps.
>
> - Joel
>

Arjen

unread,
Oct 24, 2010, 8:06:28 AM10/24/10
to Mongoid
Actually I was doing something wrong. I was using :stored_as => Array
instead of :stored_as => :array. Whoops. Now the product_ids are
stored properly using an array in the group document. Lesson well
learned :)
Reply all
Reply to author
Forward
0 new messages