Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do embedded manys work? #8

Open
queso opened this issue Sep 26, 2013 · 1 comment
Open

Do embedded manys work? #8

queso opened this issue Sep 26, 2013 · 1 comment

Comments

@queso
Copy link

queso commented Sep 26, 2013

I can't seem to figure out how to save an embedded many relation on an object.

class @SearchTerm extends Minimongoid
  @_collection: new Meteor.Collection('search_term')

  @embeds_many: [
    {name: 'results'}
  ]

When I create a SearchTerm and then push a javascript object onto results, it won't save. I would think just using push would save it? If not, I've tried called searchTerm.save() on my object, but the resulting object has empty results when I instantiate it.

@kaptron
Copy link
Contributor

kaptron commented Oct 18, 2013

Unfortunately minimongoid is still pretty basic, and hasn't been set up to be very sophisicated, so things like what you're trying to do here have a somewhat unintuitive mechanism. In order to save an embedded many, you have to perform a mongo $addToSet, so you can either do this manually on the collection, or use the push method which has been set up in minimongoid. So in your case, the minimongoid push would look like this:

search_term = SearchTerm.create {valid_data: ...}
search_term.push {results: {some_result_data: ...}}

Otherwise to perform a save, the save method requires you to pass in whatever attributes you're trying to save, it does not know what has been changed based on running a model.save() alone. In other words, every save is really more of an update, which is why there is an alias to save called update:

search_term.results.push new_result
search_term.update {results: search_term.results}

In that case you'd be overwriting the search_term.results altogether with the new array. I have to recommend though you probably want to stick to saving the data you're trying to save (option 1) and not actually pushing a minimongoid instantiated model to your save methods, otherwise you may run into the need to use JSON.decycle to prevent any circular references (i.e. search_term embeds_many results which are embedded_in search_term which embeds_many results...)

So, two things that have not been implemented in minimongoid which would be useful here are:

  1. Dirty tracking: performing a model save() with no params should be able to detect any changes
  2. Related models should act more like "model managers", so that you can do things like search_term.results.push and it will know how to handle that. Currently search_term.results is a normal JavaScript Array of embedded models, but the Array itself doesn't have any special minimongoid capabilities.

This has come up in some other issues (#2, #5, #12). This project isn't something that I'm spending a lot of time on, just something in my free time, so if anyone wants to work on some of these features and submit a pull request, that would be most awesome...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants